java_jdbc_odbc_连接封装

很久以前的一个代码,封装的jdbc连接和常用操作,包含存储过程调用

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.CallableStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.sun.corba.se.spi.orbutil.fsm.Guard.Result; public class DB { // 数据库驱动类 private final static String DRIVER_NAME = "oracle.jdbc.driver.OracleDriver"; // 数据库URL地址 private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; // 数据库用户名 private static final String USERNAME = "scott"; // 密码 private static final String PASSWORD = "tiger"; private Connection con = null; private PreparedStatement ps = null; private ResultSet rs = null; static { try { Class.forName(DRIVER_NAME); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获得数据库连接 * * @return 数据库连接 */ public Connection getConnection() throws ClassNotFoundException, SQLException { if (con == null) { con = DriverManager.getConnection(URL, USERNAME, PASSWORD); } return con; } public ResultSet executeQuery(String sql, Object...params) throws ClassNotFoundException, SQLException { con = this.getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < params.length; i++) { Object o = params[i]; if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); ps.setTimestamp(i + 1, t); } else { ps.setObject(i + 1, o); } } rs = ps.executeQuery(); return rs; } /** * 执行查询操作,返回一个结果接 * * @param sql 要执行的SQL语句 * @param list SQL参数的集合 * @return 结果集 * @throws SQLException * @throws ClassNotFoundException */ public ResultSet executeQuery(String sql, List list) throws ClassNotFoundException, SQLException { con = this.getConnection(); // 获得预编译语句对象 ps = con.prepareStatement(sql); // 传递参数 if (list != null) { for (int i = 0; i < list.size(); i++) { Object o = list.get(i); if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); ps.setTimestamp(i + 1, t); } else { ps.setObject(i + 1, o); } } } rs = ps.executeQuery(); return rs; } public int executeUpdate(String sql, Object...params) throws ClassNotFoundException, SQLException { con = this.getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < params.length; i++) { Object o = params[i]; if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); ps.setTimestamp(i + 1, t); } else { ps.setObject(i + 1, o); } } return ps.executeUpdate(); } /** * 执行增删改语句,返回受影响的行数 * @param sql sql语句 * @param list sql语句的参数集合 */ public int executeUpdate(String sql, List list) throws ClassNotFoundException, SQLException { con = this.getConnection(); ps = con.prepareStatement(sql); if (list != null) { for (int i = 0; i < list.size(); i++) { Object o = list.get(i); if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); ps.setTimestamp(i + 1, t); } else { ps.setObject(i + 1, o); } } } return ps.executeUpdate(); } /** * 无参数的存储过程 * * @throws SQLException * @throws ClassNotFoundException */ public void prepareCall(String storename, Object... params) throws ClassNotFoundException, SQLException { con = this.getConnection(); String str = " call "+ storename; cs = con.prepareCall(str); for (int i = 0; i < params.length; i++) { Object o = params[i]; if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); cs.setTimestamp(i + 1, t); } else { cs.setObject(i + 1, o); } } cs.execute(); } /** * 调用有输出参数 (输出参数类型只能为Stirng类型的数据)的存储过程 * * @throws List<Integer> 指定注册类型 * @throws SQLException * @throws ClassNotFoundException */ public List<String> prepareCall(String storename, List<Integer> list, Object.. . params) throws ClassNotFoundException, SQLException { con = this.getConnection(); String str = " call "+ storename; cs = con.prepareCall(str); List<Integer> klist = new ArrayList<Integer>(); List<String> relist = new ArrayList<String>(); if (params.length != 0) { for (int i = 0; i < params.length; i++) { Object o = params[i]; if (o instanceof Date) { Date d = (Date) o; Timestamp t = new Timestamp(d.getTime()); cs.setTimestamp(i + 1, t); } else { cs.setObject(i + 1, o); } if (params.length - 1 == i && list.size() != 0) { for (int k = 0; k < list.size(); k++) { cs.registerOutParameter((i + (k + 2)), list.get(k)); klist.add(i+(k+ 2)); } } } } cs.execute(); for(int i = 0;i<klist.size();i++){ relist.add(cs.getString(klist.get(i))); } return relist; } /** * 关闭连接 */ public void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { DB db = new DB(); String sql = "insert into tab_message values(mesid_seq.nextval,?,?,?,sysdate,0)"; try { db.executeUpdate(sql, "短信内容", "zhangsan", "admin"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { db.close(); } } }

这个是各个驱动的连接方式,需导入相应驱动的jar

Microsoft SQL Server (6.5, 7, 2000 and 2005) and Sybase (10, 11, 12). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: jTDS Home Page: http://jtds.sourceforge.net/ JDBC Ver: 1.0.2 Download: http://sourceforge.net/project/showfiles.php?group_id=33291 Conn Code: Class.forName("net.sourceforge.jtds.jdbc.Driver "); Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://host:port/database","user","password"); or Connection con = DriverManager.getConnection("jdbc:jtds:sybase://host:port/database","user","password"); Microsoft SQL Server 2000 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: SQL Server 2000 Driver for JDBC Home Page: http://www.microsoft.com/china/sql/ JDBC Ver: (SQL Server 2000 Driver for JDBC) Download: http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp Conn Code: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection conn = DriverManager.getConnection ("jdbc:microsoft:sqlserver://server1:1433;DatabaseName=database","user","password"); Oracle ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: Connector/J Home Page: http://www.oracle.com/technology/software/tech/java/ JDBC Ver: (based on Oracle) Download: http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/ Conn Code: Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:databse","user","password"); MySQL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: Connector/J Home Page: http://dev.mysql.com/ JDBC Ver: 3.1 Download: http://dev.mysql.com/downloads/ Conn Code: Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://host:port/database","user","password"); Sybase ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: jConnect Home Page: http://www.sybase.com.cn/cn/content/SDN/ JDBC Ver: 4.5/5.5 Download: http://www.sybase.com/detail_list?id=13&morenavId=8668&multi=true&SR=Y&show=1265 Conn Code: Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance(); DriverManager.getConnection("jdbc:sybase:Tds:IP:2638?ServiceName="+database,"user","password"); Postgresql ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: PostgreSQL JDBC Home Page: http://jdbc.postgresql.org/ JDBC Ver: 8.0 Build 310 Download: http://jdbc.postgresql.org/download.html Conn Code: Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection("jdbc:postgresql://host:port/database","user","password"); IBM Informix ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: Informix JDBC Driver (IBM Informix JDBC V3.0) Home Page: http://www-306.ibm.com/software/data/informix/ JDBC Ver: 3.0.JC1 Download: http://www14.software.ibm.com/webapp/download/search.jsp?go=y&rs=ifxjdbc Conn Code: Class.forName("com.informix.jdbc.IfxDriver").newInstance(); Connection conn= DriverManager.getConnection("jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";); IBM DB2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JDBC Name: DB2 JDBC Universal Driver Home Page: http://www-306.ibm.com/software/data/db2/udb/ JDBC Ver: Download: http://www-128.ibm.com/developerworks/db2/downloads/jcc/ Conn Code: Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); Connection conn= DriverManager.getConnection("jdbc:db2://localhost:5000/sample",user,password);

odbc 连接
使用 ODBC进行连接: ODBC配置:开始-->管理工具 -->数据源--> 用户DSN-->添加 -->选择Oracle in OraDb10g_home1-->完成
Data Source Name中填JdbcOdbc
Description为描述可不填
TNS Service Name中选择Oracle
User ID中填你要使用的数据库中的用户
完成配置后确定
Java文件中需要配置的东西: 定义三个值 :

Connection con = null; PreparedStatement pst = null; ResultSet rs = null;

下面的是需要处理异常的代码段 :

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); String url = "jdbc:odbc:JdbcOdbc" ; con = DriverManager.getConnection(url, "cxd", "cxd" ); String sql = "select * from code where username ='name' and userpassword ='password' "; pst = con.prepareStatement(sql); rs = pst.executeQuery(); //执行查询操作 String sql = "delete from name "; pst = con.prepareStatement(sql); pst.executeUpdate(); //执行删除操作<增删改都使用它>

//任何时候都要进行关闭

if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); }