62,623
社区成员
发帖
与我相关
我的任务
分享
import java.sql.*;
public class DataStore {
private static DataStore db = null;
private static Connection conn = null;
private Statement stmt = null;
/**
* DataStore()构造函数主要是即在数据驱动,
* 然后通过conn对象连接到数据库,
* 最后创建对数据块的访问块stmt
*
*/
private DataStore() throws Exception {
if (conn == null) {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/fileData", "root", "root");
stmt = conn.createStatement();
}
}
/**
* 用來返回唯一的DataStore實例,
*在外部調用可以返回一個DataStore的一個實例
*/
public static DataStore getInstance() {
if (db == null) {
try {
db = new DataStore();
} catch (Exception e) {
conn = null;
return null;
}
}
return db;
}
/*
传递一个String类型的变量,该函数就是对数据库的操作,
主要为插入,更新,删除等操作*/
synchronized public void execute(String sql) {
try {
if (stmt != null)
stmt.executeUpdate(sql);
} catch (SQLException e) {
}
}
/**
* 传递一个String类型的变量,该函数主要是实现对数据库中数据的查询操作
*/
synchronized public ResultSet read(String sql) {
ResultSet tmp = null;
try {
if (stmt != null) {
tmp = stmt.executeQuery(sql);
return tmp;
} else {
return null;
}
} catch (SQLException e) {
return null;
}
}
/*
* 传递一个String类型的变量
* */
synchronized public int readCount(String sql) {
int nCount = 0;
try {
if (stmt != null) {
ResultSet tmp = null;
tmp = stmt.executeQuery(sql);
if (tmp != null && tmp.next()) {
nCount = tmp.getInt(1);
} else {
nCount = 0;
}
} else {
nCount = 0;
}
} catch (SQLException e) {
nCount = 0;
}
return nCount;
}
synchronized public void stop() {
try {
if (conn != null) {
conn.close();
stmt.close();
}
} catch (Exception e) {
/* 记录到日志 */
// Log log = LogFactory.getLog("mylog");
// log.error("数据库关闭出错-DataStore.java");
} finally {
conn = null;
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 数据库连接
* @author
* @version 1.00 2006/05/15 作成<BR>
*
* @since jdk1.4.2
*
*/
public class DBFactory {
/** 创建Connection对象 */
private Connection conn = null;
/** 创建statement对象 */
private Statement stmt = null;
/** 用户名 */
private String userID;
/** 密码 */
private String password;
/** 连接字符串 */
private String url;
/**
* 构造函数
*
*/
public DBFactory() {
this.userID = "**";
this.password = "**";
this.url = "jdbc:oracle:thin:@192.168.***.***:1521:***";
}
/**
* 连接数据库
*
*/
public void connect() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, userID, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
conn.setAutoCommit(true);
System.err.println("Connect to DB: Succ!");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Connect to DB: Fail!");
}
}
/**
* 释放连接
*
*/
public void disconnect() {
try {
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行SQL语句并得到结果
*
* @return 结果集
*
* @param sql
* SQL语句
*/
public ResultSet getData(String sql) {
try {
System.out.println("sql->" + sql);
ResultSet rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error getData: " + sql);
return null;
}
}
/**
* 执行SQL语句
*
* @return 执行是否成功 <br>
* true:成功/false:失败
*
* @param sql
* SQL语句
*/
public boolean executeSQL(String sql) {
try {
System.out.println("sql->" + sql);
stmt.executeUpdate(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error executeSQL: " + sql);
return false;
}
}
}
import java.sql.*;
public class testDB {
public static void main(String args[]){
Connection conn=ConnectionFactory.getConnection();
System.out.println(conn);
}
}
import java.sql.*;
public class DBConnection
{
//jdbc driver, jdbc-odbc bridge
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//the database to be connected
String dbName = "jdbc:odbc:user";
//database's user name
String dbUser = "";
//database's password
String dbPass= "";
//connection
Connection connection = null;
//result collection
ResultSet resultSet = null;
/**
* constructor
* */
public DBConnection()
{
try
{
Class.forName( dbDriver );
}
catch( ClassNotFoundException classNotFoundException )
{
System.err.println( "DBConnection(): " + classNotFoundException.getMessage() );
}
}
/**
* execute query
* @param sql sentence
* @return the result collection
* */
public ResultSet executeQuery( String sql )
{
resultSet = null;
try
{
connection = DriverManager.getConnection( dbName, dbUser, dbPass );
Statement stmt = connection.createStatement();
resultSet = stmt.executeQuery( sql );
}
catch( SQLException sqlException )
{
System.err.println( "executeQuery:"+sqlException.getMessage() );
}
return resultSet;
}
public static void main( String[] args )
{
}
}
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ConnectionFactory {
private ConnectionFactory(){
}
private static ComboPooledDataSource ds = null;
static {
try {
Logger log = Logger.getLogger("com.mchange");
// 去掉C3P0的初始化信息
log.setLevel(Level.WARNING);
ds = new ComboPooledDataSource();
ds.setDriverClass(Config.getDriverClass());
ds.setJdbcUrl(Config.getJdbcUrl());
ds.setUser(Config.getDatabaseUsername());
ds.setPassword(Config.getDatabasePassword());
ds.setMaxPoolSize(Config.getMaxPoolSize());
ds.setMinPoolSize(Config.getMinPoolSize());
ds.setMaxStatements(Config.getMaxStatement());
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection() {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
return con;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/s2";
private static final String user = "root";
private static final String password = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}