? jdbc crud的演示代码 开元棋牌百度文库_开元棋牌玩得崩溃_933开元棋牌
?
广信学院?>>?其他信息

jdbc crud的演示代码

来源:广东信息科技职业培训学院???点击数:25166
下面是连接数据库的代码,sqlserver2000需要打上sp3的补丁,jdbc sp2的驱动 注意版本对应

  package demo;

  import java.sql.Connection;

  import java.sql.DriverManager;

  import java.sql.PreparedStatement;

  import java.sql.ResultSet;

  import java.sql.SQLException;

  public class ConnectionManager {

  private static String username = "sa";

  private static String password = "secret";

  private static String dUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";

  private static String dDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

  private static Connection conn = null;

  public static Connection getConnection() {

  try{

  Class.forName(dDriver);

  }catch(ClassNotFoundException e){

  e.printStackTrace();

  }

  try{

  conn=DriverManager.getConnection(dUrl,username,password);

  }catch(Exception e){

  e.printStackTrace();

  }

  return conn;

  }

  public void rollback() {

  try {

  conn.rollback();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  }

  public void commit() {

  try {

  conn.commit();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  }

  public static void close(ResultSet rs, PreparedStatement pstmt,

  Connection conn) {

  try {

  if (rs != null) {

  rs.close();

  }

  if (pstmt != null) {

  pstmt.close();

  }

  if (conn != null) {

  conn.close();

  }

  } catch (SQLException e) {

  e.printStackTrace();

  }

  }

  }


?