dbconn.java~3~
资源名称:JSP02.rar [点击查看]
上传用户:top0756
上传日期:2022-08-11
资源大小:6501k
文件大小:4k
源码类别:
Jsp/Servlet
开发平台:
VBScript
- package proj112;
- import java.io.*;
- //import javax.servlet.http.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class dbconn {
- String Sd="sun.jdbc.odbc.JdbcOdbcDriver"; //建立一个联接机
- String url="jdbc:odbc:dsn112"; //建立一个Odbc源
- Connection con=null; //Connection对象
- ResultSet rs=null; //建立一个记录集
- PreparedStatement prepstmt = null;
- String userName = "sa";
- String password = "";
- public dbconn(){
- try{
- Class.forName(Sd); //用classforname方法加载驱动程序类
- }catch(java.lang.ClassNotFoundException e){ //当没有发现这个加载这个类的时候抛出的异常
- System.err.println(e); //执行系统的错误打印
- }
- }
- public ResultSet executeQuery(String sql){ //可以执行添加删等操作
- try{
- con=DriverManager.getConnection(url,userName,password);
- Statement stmt = con.createStatement(
- ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs=stmt.executeQuery(sql);
- }catch(SQLException er){
- System.err.println(er.getMessage());
- }
- return rs;
- }
- public int executeUpdate(String sql){ //数据库的更新操作
- int result=0;
- try{
- con=DriverManager.getConnection(Sc,userName,password);
- Statement stmt=con.createStatement();
- result=stmt.executeUpdate(sql);
- }catch(SQLException ex){
- System.err.println(ex.getMessage());
- }
- return result;
- }
- public void close(){
- try{
- if(con!=null)
- con.close();
- }catch(Exception e){
- System.out.print(e);
- }try{
- if(rs!=null)
- rs.close();
- }catch(Exception e){
- System.out.println(e);
- }
- }
- public PreparedStatement preparedstatements(String sql) {
- try{
- con=DriverManager.getConnection(Sc,userName,password);
- prepstmt = con.prepareStatement(sql);
- //PreparedStatement prepstmt = null;
- }catch(SQLException ex){
- System.err.println(ex.getMessage());
- }
- return prepstmt;
- }
- /* public static void main(String[] args) {
- try {
- ConnDB db = new ConnDB();
- //db.executeQuery("select * from student_info";
- ResultSet rs = db.executeQuery("select * from student_info");
- while(rs.next()) {
- System.out.println(rs.getShort(1));
- System.out.println(rs.getString(2));
- }
- //System.out.println("连接数据库成功!");
- }catch(Exception e) {
- e.printStackTrace();
- }
- }*/
- }