jdbc连接,方便自己查阅

来源:互联网 发布:阿里云深圳机房ip地址 编辑:程序博客网 时间:2024/06/05 16:34
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class BaseDao{        protected Connection conn=null;    protected PreparedStatement prst=null;    protected ResultSet rs=null;        private final String connString="jdbc:sqlserver://localhost;databasename=xxx";    private final String user="xxx";    private final String pwd="xxxx";        protected void openConn(){        try {            //SQLSERVER            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");            //ORACLE            Class.forName("Oracle.jdbc.driver.OracleDriver");            //mysql            Class.forName("com.mysql.jdbc.Drive");            conn=DriverManager.getConnection(connString, user, pwd);        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }    }        protected void closeConn(){        try {            if(this.prst!=null)                prst.close();            if(this.rs!=null)                rs.close();            if(this.conn!=null)                conn.close();        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }    }        protected void executeUpdate(String sql, Object... para){        try {            prst = conn.prepareStatement(sql);            for (int i = 0; i < para.length; i++) {                prst.setObject(i + 1, para[i]);            }            prst.executeUpdate();        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();            throw new RuntimeException(e.getMessage());        }    }        protected ResultSet executeQuery(String sql, Object... para) {        try {            prst = conn.prepareStatement(sql);            for (int i = 0; i < para.length; i++) {                prst.setObject(i + 1, para[i]);            }            this.rs = prst.executeQuery();        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }                 return this.rs;    }}
0 0
原创粉丝点击