首页上一页 1 下一页尾页 1 条记录 1/1页
Java连接SQLServer的问题。
发表在Java图书答疑
2009-03-05
是否精华
是
否
版块置顶:
是
否
public class JDBConnection {
private String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatebaseName=db_shopping";
public Connection connection = null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance(); // Load database driver
connection = DriverManager.getConnection(url); // Load
// database
// connection.setAutoCommit(true);
} catch (Exception e) {
System.out.println("Error to load database!");
}
}
}
public class MemberDao {
private Connection connection = null;
private PreparedStatement ps = null;
JDBConnection jdbc = null;
public MemberDao() {
jdbc = new JDBConnection();
connection = jdbc.connection;
}
public boolean updatePassword(String password, Integer id) {
try {
ps = connection
.prepareStatement("update tb_member set password=? where id=?");
ps.setString(1, password);
ps.setInt(2, id.intValue());
ps.executeUpdate();
ps.close();
// connection.commit();
return true;
} catch (SQLException ex) {
return false;
}
}
}
public class TestDao extends TestCase {
public void testDao() throws SQLException {
MemberDao mem = new MemberDao();
mem.updatePassword("HappyNewYear", Integer.valueOf(19));
}
}
以上代码执行无错误,为什么数据库里的值没有改变?而如下代码则可行:
public class TestData {
public static void main(String[] args) throws SQLException, Exception {
/*try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=db_shopping");
Statement stmt = conn.createStatement();
String sql = "select * from tb_member";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id"));
}
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}
private String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatebaseName=db_shopping";
public Connection connection = null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance(); // Load database driver
connection = DriverManager.getConnection(url); // Load
// database
// connection.setAutoCommit(true);
} catch (Exception e) {
System.out.println("Error to load database!");
}
}
}
public class MemberDao {
private Connection connection = null;
private PreparedStatement ps = null;
JDBConnection jdbc = null;
public MemberDao() {
jdbc = new JDBConnection();
connection = jdbc.connection;
}
public boolean updatePassword(String password, Integer id) {
try {
ps = connection
.prepareStatement("update tb_member set password=? where id=?");
ps.setString(1, password);
ps.setInt(2, id.intValue());
ps.executeUpdate();
ps.close();
// connection.commit();
return true;
} catch (SQLException ex) {
return false;
}
}
}
public class TestDao extends TestCase {
public void testDao() throws SQLException {
MemberDao mem = new MemberDao();
mem.updatePassword("HappyNewYear", Integer.valueOf(19));
}
}
以上代码执行无错误,为什么数据库里的值没有改变?而如下代码则可行:
public class TestData {
public static void main(String[] args) throws SQLException, Exception {
/*try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=db_shopping");
Statement stmt = conn.createStatement();
String sql = "select * from tb_member";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id"));
}
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}