j2ee和mysql怎么连接_J2EE数据库连接不再烦恼
剛開始接觸j2ee的時候總是為數據庫的開關連接問題而煩惱,雖然問題很簡單卻很是瑣碎,于是干脆寫成一個類將所有必要的基本操作全部總結進去,以后只要輕松的import一下就可以了啊:)菜鳥們enjoying!
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
public class DBConnection {
//數據庫屬性
public static String dbDriverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//其他驅動自己填寫
public static String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="DBNAME";
public static String dbUserID = "userid";
public static String dbUserPass = "userpassword";
public DBConnection() {
}
public void open() throws Exception{
open( dbDriverName, dbURL, dbUserID, dbUserPass);
}
/**
* 創建一個數據庫連接
* @param driver
* JDBC驅動類
* @param url
* 數據庫位置
* @param userID
* 數據庫登錄名
* @param password
* 數據庫登錄密碼
* @throws Exception
* 創建數據庫失敗
*/
public void open(String driver, String url,
String userID, String password)throws Exception{
close();
Class.forName(driver);
connection = java.sql.DriverManager.getConnection(url, userID, password);
connected = true;
}
/**
* 關閉當前數據庫連接
*/
public void close(){
if (connection == null )
return;
try{
if ( !connection.isClosed() )
connection.close();
}catch(Exception e){
}
connected = false;
}
/**
* 判斷連接狀態
* @return
* 連接返回true,否則返回false;
*/
public boolean isConnected(){
return connected;
}
public void executeInsert (String sql) throws Exception
{
check();
createStatement();
statement.execute(sql);
}
public void executeBatch(Vector sqls)throws Exception{
check();
createStatement();
for( int i = 0; i < sqls.size(); i++ ){
String sql = (String)sqls.get(i);
if ( sql.toLowerCase().trim().startsWith("select")){
throw new Exception("Batch 操作不支持Select操作");
}
statement.addBatch(sql);
}
statement.executeBatch();
}
public ResultSet executeSelect (String sql) throws Exception
{
check();
createStatement();
statement.execute(sql);
return statement.getResultSet();
}
public int executeUpdate (String sql) throws Exception
{
check();
createStatement();
statement.execute(sql);
return statement.getUpdateCount();
}
public int executeDelete (String sql) throws Exception
{
check();
createStatement();
statement.execute(sql);
return statement.getUpdateCount();
}
public void execute (String sql) throws Exception
{
check();
createStatement();
statement.execute(sql);
}
public void beginTrans ()throws Exception
{
check();
connection.setAutoCommit(false);
}
public void commit () throws Exception
{
check();
connection.commit();
}
public void rollback ()
{
try{
connection.rollback();
}catch(Exception e){
}
}
public boolean isConnectioned () throws Exception
{
return connected;
}
protected void check () throws Exception
{
if (!connected)
throw new Exception("沒有創建數據庫連接");
}
public void createStatement() throws Exception
{
if ( statement == null || connection.getAutoCommit() )
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
protected Connection connection = null;
protected boolean connected;
protected Statement statement;
}
總結
以上是生活随笔為你收集整理的j2ee和mysql怎么连接_J2EE数据库连接不再烦恼的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python2转python3代码_2t
- 下一篇: 职高计算机选修6知识点,(计算机基础考试