?? dbconn.java
字號:
package lee;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import org.apache.commons.dbcp.BasicDataSource;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class DBConn
{
private static DBConn dc;
private Connection conn = null;
private Statement stmt = null;
private DBConn()
{
}
public static DBConn instance()
{
if (dc == null)
{
dc = new DBConn();
}
return dc;
}
public Statement openStmt()
{
if (stmt == null)
{
conn = getConn();
try
{
stmt = conn.createStatement();
}
catch (Exception e)
{
System.err.println("創建Statement異常: " + e.getMessage());
}
}
return stmt;
}
public void closeStmt()
{
if (stmt != null)
{
try
{
stmt.close();
}
catch (Exception e)
{
System.err.println("Statement關閉異常");
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (Exception e)
{
System.err.println("數據庫關閉異常");
}
}
}
private Connection getConn()
{
if (conn == null)
{
try
{
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/j2ee");
ds.setUsername("root");
ds.setPassword("32147");
conn = ds.getConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return conn;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -