一个手机站点采用WML 等编写开发,具体可以看代码,比较简单
源代码在线查看: dbutil.java
//Source file: D:\\WORK\\1G项目实践\\WSMS\\CI\\CODE\\src\\com\\intacpurun\\wsms\\comm\\db\\DbUtil.java
package com.intacpurun.wsms.comm.db;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import com.intacpurun.wsms.comm.*;
/**
* 数据库数据操作工具类
*/
public class DbUtil
{
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* @roseuid 425337030242
*/
private DbUtil()
{
conn = getConnection();
}
/**
* @return cn.ac.ia.hitic.platform.dao.DbUtil
* @roseuid 426DDEE80186
*/
public static DbUtil open()
{
return new DbUtil();
}
/**
* @return boolean
* @roseuid 426DDEED0213
*/
public boolean close()
{
boolean isClose = true;
//关闭ResultSet
try
{
if (rs != null)
{
rs.close();
rs = null;
}
} catch (SQLException ex)
{
rs = null;
isClose = false;
System.out.println("error happened when closing ResultSet " +
ex.getMessage());
}
//关闭PreparedStatement
try
{
if (pstmt != null)
{
pstmt.close();
pstmt = null;
}
} catch (SQLException ex1)
{
pstmt = null;
isClose = false;
System.out.println("error happened when closing preparedstatement " +
ex1.getMessage());
}
//关闭Connection
try
{
if (conn != null)
{
conn.close();
conn = null;
}
} catch (SQLException ex2)
{
conn = null;
isClose = false;
System.out.println("error happened when closing connection " +
ex2.getMessage());
}
return isClose;
}
/**
* 执行查询语句,获得查询接口
* @param sql
* @return ResultSet
* @roseuid 4251F05D009C
*/
public ResultSet executeQuery(String sql)
{
System.out.println("conn>...:" + conn);
try
{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (Exception e)
{
System.out.println("error sql ===>" + sql);
System.out.println(e.getMessage());
e.printStackTrace();
close();
}
return rs;
}
/**
* 执行修改语句,返回修改接口
* @param sql
* @return int
* @roseuid 4251F08B00AB
*/
public int executeUpdate(String sql)
{
int result = -1;
try
{
pstmt = conn.prepareStatement(sql);
result = pstmt.executeUpdate();
} catch (Exception e)
{
System.out.println("error sql ===>" + sql);
System.out.println(e);
e.printStackTrace();
close();
}
return result;
}
/**
* 获得数据库链接
* @return Connection
* @roseuid 4251F0090148
*/
private Connection getConnection()
{
Connection conn= null;
try
{
// Context context = (Context) (new InitialContext()).lookup("java:comp/env");
// DataSource ds = (DataSource) context.lookup(Constants.WSMS_DATASOURCE);
// this.conn = ds.getConnection();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:"+ Constants.WSMS_DATASOURCE,"wsms","wsms");
} catch (Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
return conn;
}
/**
* 提交并释放连接
*/
public boolean commit()
{
boolean isCommit = true;
try
{
conn.commit();
} catch (Exception e)
{
isCommit = false;
System.out.println("commit failed " + e.getMessage());
e.printStackTrace();
} finally
{
close();
}
return isCommit;
}
/**
* 修改提交方式为手动提交
*/
public boolean setAutoCommitFalse()
{
boolean isSetOK = true;
try
{
conn.setAutoCommit(false);
} catch (Exception e)
{
isSetOK = false;
close();
System.out.println("setAutoCommit failed " + e.getMessage());
e.printStackTrace();
}
return isSetOK;
}
/**
* 回滚并释放连接
*/
public boolean rollBack()
{
boolean isRollBack = true;
try
{
conn.rollback();
} catch (Exception e)
{
isRollBack = false;
System.out.println("rollbak failed " + e.getMessage());
e.printStackTrace();
}
return isRollBack;
}
}
|
相关资源 |
|
-
一个手机站点采用WML 等编写开发,具体可以看代码,比较简单
-
这是一个词法分析器,采用C++语言编写,具有很强的分析功能
-
一个在DOS下,用TC2.0开发的,可以飞梭旋扭操作.键盘操作。对于初始化中断学习算法分析等很有帮助.
-
一个很好的用java编写的论坛,可以作为开发者的参考
-
J2EE网络书店系统
采用JSP , servlet ,javabean开发
初学者可以参考
-
用java编写的一个网络考试系统.代码比较简单易懂,可供初学者借鉴.
-
一个词法分析的小程序 用vc++6.0做的 比较简单 还能用
-
学习JAVA语言时候做的一个小东西
有学c或c++的可以看一下有多大的不同
|