日期:2014-05-20 浏览次数:21131 次
//对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeUpdaterError!");
}
return true;
}
//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
//关闭数据库的操作
public void closeConnection() {
if (con != null) {
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println("Failed to close connection!");
}
finally {
con = null;
}
}
}
------解决方案--------------------
public class stuDao
{
private static Connection con = DBhpler.getCon();
public void getResultSet()
{
PreparedStatement ps = null;
ResultSet rs = null;
try
{
con.setAutoCommit(false);
ps = con.prepareStatement("select * from test where id=?");
ps.setInt(1, 12);
rs = ps.executeQuery();
}
catch (SQLException e)