DatabaseUtil
时间:2010-07-06 来源:mozhx
package com.my.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource; public class DB {
//使用数据源方式,获得一个Connection对象
public static Connection getConn_ds() {
try {
Context ctx = new InitialContext();
Context envctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/newspager");
return ds.getConnection();
} catch (Exception e) {
System.out.println("getConnection: "+e.getMessage());
return null;
}
}
//获得一个Connection对象
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加上zeroDateTimeBehavior=convertToNull解决了sql查询日期字段时当值为0000-00-00出错的问题
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newspaper?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull", "root" , "a12345");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/*
* 关闭Connection
*/
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
//关闭Statement
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
//关闭ResultSet
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource; public class DB {
//使用数据源方式,获得一个Connection对象
public static Connection getConn_ds() {
try {
Context ctx = new InitialContext();
Context envctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/newspager");
return ds.getConnection();
} catch (Exception e) {
System.out.println("getConnection: "+e.getMessage());
return null;
}
}
//获得一个Connection对象
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加上zeroDateTimeBehavior=convertToNull解决了sql查询日期字段时当值为0000-00-00出错的问题
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newspaper?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull", "root" , "a12345");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/*
* 关闭Connection
*/
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
//关闭Statement
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
//关闭ResultSet
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
相关阅读 更多 +