package common;import java.sql.*;
import javax.naming.*;public class DBBean{
private Connection con;
private Statement stmt;
private ResultSet rs;
public DBBean(){} // 得到连接
public Connection getConnection()
throws Exception
{
// 连接数据库的URL
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ChooseCourse23";
//用户名
String dbuser="sa";
//口令
String dbpass="890923";
if(con == null){
// 加载驱动程序,参数是驱动程序的名字
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// 建立连接,第一个参数是url,
// 第二个是用户名,第三个参数是口令
con=DriverManager.getConnection(url,dbuser,dbpass);
}
return con;
} // 执行有结果集返回的sql语句
public ResultSet executeQuery(String sql) throws Exception{
if(con==null)
throw new Exception("没有连接对象可用");
// 创建语句对象
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
return rs;
} // 执行更新语句
public int executeUpdate(String sql) throws Exception{
if(con == null)
throw new Exception("没有连接可用");
// 创建语句对象
stmt = con.createStatement();
// 执行sql语句
int i=0;
i=stmt.executeUpdate(sql);
con.commit();
return i;
} public void close(){
try{ if(rs!=null) rs.close(); }catch(Exception e){}
try{ stmt.close(); }catch(Exception e){}
try{ con.close(); }catch(Exception e){}
}
}上面是我的用于连接数据库的函数,从数据库中获取数据没问题,可插入数据就一直不行。
请各位高手帮我看看以下这个函数有什么问题:
// 执行更新语句
public int executeUpdate(String sql) throws Exception{
if(con == null)
throw new Exception("没有连接可用");
// 创建语句对象
stmt = con.createStatement();
// 执行sql语句
int i=0;
i=stmt.executeUpdate(sql);
con.commit();
return i;
}谢谢各位!
import javax.naming.*;public class DBBean{
private Connection con;
private Statement stmt;
private ResultSet rs;
public DBBean(){} // 得到连接
public Connection getConnection()
throws Exception
{
// 连接数据库的URL
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ChooseCourse23";
//用户名
String dbuser="sa";
//口令
String dbpass="890923";
if(con == null){
// 加载驱动程序,参数是驱动程序的名字
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// 建立连接,第一个参数是url,
// 第二个是用户名,第三个参数是口令
con=DriverManager.getConnection(url,dbuser,dbpass);
}
return con;
} // 执行有结果集返回的sql语句
public ResultSet executeQuery(String sql) throws Exception{
if(con==null)
throw new Exception("没有连接对象可用");
// 创建语句对象
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
return rs;
} // 执行更新语句
public int executeUpdate(String sql) throws Exception{
if(con == null)
throw new Exception("没有连接可用");
// 创建语句对象
stmt = con.createStatement();
// 执行sql语句
int i=0;
i=stmt.executeUpdate(sql);
con.commit();
return i;
} public void close(){
try{ if(rs!=null) rs.close(); }catch(Exception e){}
try{ stmt.close(); }catch(Exception e){}
try{ con.close(); }catch(Exception e){}
}
}上面是我的用于连接数据库的函数,从数据库中获取数据没问题,可插入数据就一直不行。
请各位高手帮我看看以下这个函数有什么问题:
// 执行更新语句
public int executeUpdate(String sql) throws Exception{
if(con == null)
throw new Exception("没有连接可用");
// 创建语句对象
stmt = con.createStatement();
// 执行sql语句
int i=0;
i=stmt.executeUpdate(sql);
con.commit();
return i;
}谢谢各位!
应该用getConnection()返回的值
// 执行更新语句
public int executeUpdate(String sql) throws Exception{
if(con == null)
throw new Exception("没有连接可用");
// 创建语句对象
stmt = con.createStatement();
// 执行sql语句
int i=0;
i=stmt.executeUpdate(sql);
con.commit();
return i;
}
你没有创建事务,怎么提交呢?在i=stmt.executeUpdate(sql)前面加上conn.setAutoCommit(false)试试。再说一条sql语句也没有必要用事务。