dbconn.java~2~
上传用户:top0756
上传日期:2022-08-11
资源大小:6501k
文件大小:4k
源码类别:

Jsp/Servlet

开发平台:

VBScript

  1. package proj112;
  2. import java.io.*;
  3. //import javax.servlet.http.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. public class dbconn {
  11.   String Sd="sun.jdbc.odbc.JdbcOdbcDriver";                //建立一个联接机
  12.           String Sc="jdbc:odbc:dsn112";                      //建立一个Odbc源
  13.           Connection con=null;                               //Connection对象
  14.           ResultSet rs=null;           //建立一个记录集
  15.           PreparedStatement prepstmt = null;
  16.           String userName = "sa";
  17.           String password = "";
  18.           public dbconn(){
  19.                   try{
  20.                           Class.forName(Sd);                  //用classforname方法加载驱动程序类
  21.                   }catch(java.lang.ClassNotFoundException e){ //当没有发现这个加载这个类的时候抛出的异常
  22.                           System.err.println(e);              //执行系统的错误打印
  23.                   }
  24.           }
  25.           public ResultSet executeQuery(String sql){         //可以执行添加删等操作
  26.                   try{
  27.                           con=DriverManager.getConnection(Sc,userName,password);
  28.                           Statement  stmt = con.createStatement(
  29.                                           ResultSet.TYPE_SCROLL_SENSITIVE,
  30.                                           ResultSet.CONCUR_READ_ONLY);
  31.                           rs=stmt.executeQuery(sql);
  32.                   }catch(SQLException er){
  33.                           System.err.println(er.getMessage());
  34.                   }
  35.                   return rs;
  36.           }
  37.           public int executeUpdate(String sql){                //数据库的更新操作
  38.                   int result=0;
  39.                   try{
  40.                           con=DriverManager.getConnection(Sc,userName,password);
  41.                           Statement stmt=con.createStatement();
  42.                           result=stmt.executeUpdate(sql);
  43.                   }catch(SQLException ex){
  44.                           System.err.println(ex.getMessage());
  45.                   }
  46.                   return result;
  47.           }
  48.           public void close(){
  49.                   try{
  50.                           if(con!=null)
  51.                                   con.close();
  52.                   }catch(Exception e){
  53.                           System.out.print(e);
  54.                   }try{
  55.                           if(rs!=null)
  56.                                   rs.close();
  57.                   }catch(Exception e){
  58.                           System.out.println(e);
  59.                   }
  60.           }
  61.           public PreparedStatement preparedstatements(String sql) {
  62.                   try{
  63.                   con=DriverManager.getConnection(Sc,userName,password);
  64.                   prepstmt = con.prepareStatement(sql);
  65.                   //PreparedStatement prepstmt = null;
  66.           }catch(SQLException ex){
  67.                   System.err.println(ex.getMessage());
  68.           }
  69.           return prepstmt;
  70.           }
  71. /*          public static void main(String[] args) {
  72.                 try {
  73.                         ConnDB db = new ConnDB();
  74.                         //db.executeQuery("select * from student_info";
  75.                         ResultSet rs = db.executeQuery("select * from student_info");
  76.                         while(rs.next()) {
  77.                                 System.out.println(rs.getShort(1));
  78.                                 System.out.println(rs.getString(2));
  79.                         }
  80.                         //System.out.println("连接数据库成功!");
  81.                 }catch(Exception e) {
  82.                         e.printStackTrace();
  83.                 }
  84.         }*/
  85. }