日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

j2ee和mysql怎么连接_J2EE数据库连接不再烦恼

發布時間:2024/7/5 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据库连接不再烦恼的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。