<strike id="ctpnt"></strike>
    1. ?
      廣信學院 >> 其他信息

      jdbc crud的演示代碼

      來源:廣東信息科技職業培訓學院   點擊數:25568
      下面是連接數據庫的代碼,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();

        }

        }

        }


      ?
      捕鱼送金币