?? batchdbconnectionutil.java
字號:
package com.dut.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* BatchDBConnectionUtil.java
* 數據庫連接操作類
* 主要方法包括getConnection(),executeQuery(),executeUpdate(),close()
* @version 1.0
* @author 楊麗娟 2006-04-03
*
*/
public class BatchDBConnectionUtil {
String DBIP = "localhost"; //數據庫IP
String DBname = "team2"; //數據庫名
String dbDriver = "com.mysql.jdbc.Driver"; //數據庫連接驅動
String strCon = null;
String USER = "root"; //數據庫用戶名
String PWD = "team2"; //數據庫密碼
Connection con = null;
ResultSet rs = null;
Statement stmt = null;
/**
* 構造関數、データバンクのコネクションを取得
* BatchDBConnectionUtil.java
* @version 1.0
* @author 楊麗娟 2006-04-03
*/
public BatchDBConnectionUtil()
{
strCon = "jdbc:mysql://" + DBIP + "/" + DBname
+ "?user=" + USER + "&password=" + PWD;
this.getConnection();
}
/**
* 獲得數據庫連接
* 將數據庫連接狀況返回
* @return String
* @version 1.0
* @author 楊麗娟 2006-04-03
*/
public String getConnection(){
String message = null;
try
{
Class.forName(dbDriver);
}
catch(java.lang.ClassNotFoundException e)
{
message = e.getMessage();
}
try {
con = DriverManager.getConnection(strCon);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message = "數據庫連接は失敗です" +e.getMessage();
System.out.println(message);
}
return message;
}
/**
* この関數の作用はデータ檢索します
* 一つSQL檢索の語句が接受する、檢索結果を引き返する?
* @param String strSQL
* @return ResultSet
* @version 1.0
* @author 楊麗娟? 2006-04-03
*/
public ResultSet executeQuery(String strSQL)
{
rs = null;
try
{
rs = stmt.executeQuery(strSQL);
}
catch(SQLException ex)
{
System.err.println("データの檢索は失敗です" +ex.getMessage());
return null;
}
return rs;
}
/**
* この関數の作用はデータべスに加える、修正する?
* 一つSQL操作の語句が接受する、操作の狀況を引き返する?
* @param String strSQL
* @return String
* @version 1.0
* @author 楊麗娟 2006-04-03
*/
public String executeUpdate(String strSQL)
{
String message = null;
try
{
stmt.executeUpdate(strSQL);
return message;
}
catch(SQLException ex)
{
message = "データ操作は失敗です" +ex.getMessage();
return message;
}
}
/**
* この関數の作用はデータべス連接を閉まります
* @version 1.0
* @author 楊麗娟 2006-04-03
*/
public void close(){
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -