Jdbc操作
分类:J2EE
来源:网络
时间:2010-10-28 23:30:21
- package cn.org.jshuwei.j2ee.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- *
- * Jdbc操作的工具类
- *
- * @author huwei(jshuwei.org.cn)
- * @since 1.0
- *
- */
- public class JdbcUtil {
- static {
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Class.forName("com.mysql.jdbc.Driver");
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * 得到Connection
- *
- * @since 1.0
- * @param dbType
- * 数据库类型(oracle/mysql)
- * @return 返回Connection
- */
- public static Connection getConnection(String dbType) {
- String url = "";
- String user = "";
- String password = "";
- if (dbType.equals("oracle")) {
- url = "jdbc:oracle:thin:@localhost:6666:XE";
- user = "jshuwei";
- password = "123456";
- }
- if (dbType.equals("mysql")) {
- url = "jdbc:mysql://localhost:3306/test";
- user = "jshuwei";
- password = "123456";
- }
- if (dbType.equals("sqlServer")) {
- url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test";
- user = "jshuwei";
- password = "123456";
- }
- try {
- return DriverManager.getConnection(url, user, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 打印记录集对象
- *
- * @since 1.0
- * @param rs
- * 需要打印的记录集对象
- */
- public static void printRs(ResultSet rs) {
- if (rs == null) {
- System.out.println("ResultSet is null!");
- return;
- }
- try {
- ResultSetMetaData md = rs.getMetaData();
- int cols = md.getColumnCount();
- for (int i = 1; i <= cols; i++) {
- // 列名,类型编号,类型名称
- System.out
- .println(md.getColumnName(i) + "-->"
- + md.getColumnType(i) + "-->"
- + md.getColumnTypeName(i));
- }
- System.out.println("=========================================");
- while (rs.next()) {
- for (int i = 1; i <= cols; i++) {
- System.out.println(md.getColumnName(i) + "="
- + rs.getString(i) + " ");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 释放资源
- *
- * @since 1.0
- * @param rs
- * 需要释放的记录集对象
- * @param stmt
- * 需要释放的Statement对象
- * @param con
- * 需要释放的连接对象
- */
- public static void release(ResultSet rs, Statement stmt, Connection con) {
- if (rs != null)
- try {
- rs.close();
- } catch (Exception e) {
- }
- if (stmt != null)
- try {
- stmt.close();
- } catch (Exception e) {
- }
- if (con != null)
- try {
- con.close();
- } catch (Exception e) {
- }
- }
- /**
- * 释放资源
- *
- * @since 1.0
- * @param o
- * 需要释放的对象
- */
- public static void release(Object o) {
- try {
- if (o instanceof ResultSet) {
- ((ResultSet) o).close();
- } else if (o instanceof Statement) {
- ((Statement) o).close();
- } else if (o instanceof Connection) {
- ((Connection) o).close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- 默认分类(20)
- J2EE(25)
- Java(56)
- PHP(55)
- SEO(10)
- 网页设计(20)
- 网站建设(37)
- 数据库(7)
- JavaScript(17)
- JQuery(6)
- MySQL(20)
- SQL Server(6)
- Access(1)
- Oracle(6)
- office(6)
- Dreamweaver(4)
- Photoshop(12)
- Flash(9)
- Fireworks(13)
- CSS(14)
- HTML(4)
- .NET(7)
- ASP(2)
- DB2(1)
- Ajax(2)
- Linux(12)
- Struts(7)
- Hibernate(8)
- Spring(2)
- Jsp(22)
- Asp(8)
- C#(3)
- C++(1)
- 网络安全(5)
- 软件工程(7)
- XML(1)
- English(2)
- 计算机等级考试(2)
- 计算机病毒(4)
- 个人日志(76)
- 互联网(15)
- ActionScript(10)
- Android(3)
- 数据结构与算法(1)
- 游戏策略(3)
- 美文翻译(2)
- 编程开发(19)
- 计算机应用(4)
- 计算机(10)
- Unity3d(6)
- 其他(1)
- egret(1)