- /**
- * Title: 數據庫連接及操作類
- * Description: 加載各種數據庫驅動程序、連接數據庫、提交SQL及返回結果集。
- * @date : 2006-4-3
- * @author : zhanglei
- * @version : 1.0
- */
- package server;
- import java.sql.*;
- import java.util.*;
- import java.io.*;
- //import oracle.sql.*;
- public class JdbcWork {
- public static Connection conn = null;
- private Statement stmt = null;
- private DomXML DomXML = MainServer.DomXML;
- public Connection ConnectDB() {
- String database = "";
- String driverName = "";
- String databaseURL = "";
- String user = "";
- String password = "";
- try {
- DomXML.GetBeginItem("JdbcConfig");
- database = DomXML.GetItemVal("database");
- DomXML.GetBeginItem(database);
- driverName = DomXML.GetItemVal("driverName");
- databaseURL = DomXML.GetItemVal("databaseURL");
- user = DomXML.GetItemVal("username");
- password = DomXML.GetItemVal("password");
- }
- catch (Throwable t) {
- SaveLog.SetInfo("數據庫配置文件讀取錯誤!", 1);
- return null;
- }
- try {
- Class.forName(driverName);//安裝驅動程序
- //System.out.println ("drive ok");
- }
- catch (java.lang.ClassNotFoundException e) {
- SaveLog.SetInfo("加載JDBC驅動失敗!", 1);
- SaveLog.SetInfo(e.getMessage(), 2);
- return null;
- }
- try {
- conn = java.sql.DriverManager.getConnection(databaseURL, user, password);
- conn.setAutoCommit(false);
- //System.out.println ("conn ok");
- return conn;
- }
- catch (java.sql.SQLException e) {
- SaveLog.SetInfo("數據庫連接失敗!", 2);
- SaveLog.SetInfo("SQLException: " + e.getMessage(), 2);
- return null;
- }
- }
- public int ExecSql(String sql) {
- Statement sqlStatement = null;
- if (conn == null) {
- // SaveLog.SetInfo("ExecSql conn NULL", 2);
- conn = ConnectDB();
- }
- try {
- sqlStatement = conn.createStatement();
- try {
- sqlStatement.executeUpdate(sql);
- sqlStatement.close();
- conn.commit();
- //System.out.println (sql+"執行成功!");
- return 1;
- }
- catch (SQLException ex) {
- SaveLog.SetInfo(sql + " ExecSql1:" + ex, 2);
- conn.rollback();
- }
- }
- catch (SQLException ex) {
- SaveLog.SetInfo(sql + " ExecSql2:" + ex, 2);
- }
- return -1;
- }
- public int PstmtExecSql(String sql) {
- PreparedStatement pstmt = null;
- if (conn == null) {
- // SaveLog.SetInfo("PstmtExecSql conn NULL", 2);
- conn = ConnectDB();
- }
- try {
- try {
- pstmt = conn.prepareStatement(sql);
- pstmt.executeUpdate();
- pstmt.close();
- //System.out.println (sql+"執行成功!");
- return 0;
- }
- catch (SQLException e) {
- conn.rollback();
- SaveLog.SetInfo(sql + " PstmtExecSql1:" + e, 2);
- return -1;
- }
- }
- catch (SQLException ex) {
- SaveLog.SetInfo(sql + " PstmtExecSql:2" + ex, 2);
- }
- return -1;
- }
- /**
- *PreparedStatement 執行SQL語句,並提交
- *@param sql SQL語句
- */
- public int PstmtExecSql(Vector sql, Vector zp) {
- int blobflag = 0;
- /* if (zp.size() == sql.size()) {
- flag = 1;
- }
- }
- */
- sun.misc.BASE64Decoder base64Decoder = new sun.misc.BASE64Decoder();
- byte b[] = null;
- InputStream is = null; //= new ByteArrayInputStream()
- PreparedStatement pstmt = null;
- String Sql = null;
- // SaveLog.SetInfo("insert into table start", 2);
- if (conn == null) {
- SaveLog.SetInfo("PstmtExecSql Vconn NULL", 2);
- conn = ConnectDB();
- }
- try {
- try {
- for (int j = 0; j < sql.size(); j++) {
- Sql = sql.get(j).toString();
- if (Sql.indexOf(" ? ") > 1) {
- blobflag = 1;
- }
- else {
- blobflag = 0;
- }
- pstmt = conn.prepareStatement(sql.get(j).toString());
- if (blobflag == 1) {
- try {
- b = base64Decoder.decodeBuffer(zp.get(j).toString());
- is = new ByteArrayInputStream(b);
- pstmt.setBinaryStream(1, is, is.available());
- }
- catch (IOException e) {
- System.out.println("BLOB Error:" + e);
- }
- }
- pstmt.executeUpdate();
- pstmt.close();
- }
- return 0;
- }
- catch (SQLException e) {
- conn.rollback();
- SaveLog.SetInfo(Sql + " VPstmtExecSql1:" + e, 2);
- return -1;
- }
- }
- catch (SQLException ex) {
- SaveLog.SetInfo(Sql + " VPstmtExecSql2:" + ex, 2);
- return -1;
- }
- }
- /**
- *提交PreparedStatement 的SQL語句
- */
- public int PstmtCommit() {
- if (conn == null) {
- conn = ConnectDB();
- }
- try {
- try {
- conn.commit();
- return 0;
- }
- catch (SQLException e) {
- SaveLog.SetInfo("PstmtCommit1:" + e, 2);
- conn.rollback();
- return -1;
- }
- }
- catch (SQLException ex) {
- SaveLog.SetInfo("PstmtCommit2:" + ex, 2);
- return -1;
- }
- }
- /**
- *得到結果集
- *@param sql 輸入SQL語句
- *@param out 輸出ResultSet結果集
- */
- public ResultSet SelectSql(String sql) {
- if (conn == null) {
- // SaveLog.SetInfo("SelectSql conn NULL", 2);
- conn = ConnectDB();
- }
- Statement stmt = null;
- try {
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, //TYPE_SCROLL_INSENSITIVE
- ResultSet.CONCUR_READ_ONLY);
- return stmt.executeQuery(sql);
- }
- catch (Exception e) {
- SaveLog.SetInfo(sql + " SelectSql:" + e, 2);
- CloseConn();
- return null;
- }
- }
- /**
- *得到結果集
- *@param out 輸出Statment
- */
- public Statement GetStatment() {
- if (conn == null) {
- // SaveLog.SetInfo("GetStatment conn NULL", 2);
- conn = ConnectDB();
- }
- try {
- stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, //TYPE_SCROLL_INSENSITIVE
- ResultSet.CONCUR_READ_ONLY);
- return stmt;
- }
- catch (Exception e) {
- SaveLog.SetInfo(" GetStatment 異常:" + e, 2);
- CloseConn();
- return null;
- }
- }
- /**
- *關閉Statment
- */
- public void CloseStatment() {
- try {
- if (stmt != null) {
- stmt.close();
- //System.out.println("Statmentclose()");
- }
- }
- catch (Exception e) {
- SaveLog.SetInfo("CloseStatment 異常:" + e, 2);
- }
- }
- /**
- *得到記錄數 注意:在得到結果集後使用,執行後使記錄集定位到first()
- *@param sql 輸入ResultSet
- *@param out 輸出ResultSet結果集記錄數
- */
- public int GetCount(ResultSet Rs) {
- int recSum = 0;
- try {
- Rs.next();
- /* do {
- recSum++;
- }
- while (Rs.next());*/
- Rs.last();
- recSum = Rs.getRow();
- Rs.first();
- return recSum;
- }
- catch (Exception e) {
- SaveLog.SetInfo("GetCount(ResultSet) 異常:" + e, 2);
- return 0;
- }
- }
- /**
- *得到大結果集記錄數
- *@param out 輸出Statment
- */
- public int GetCount(String Sql) {
- int count = 0;
- if (conn == null) {
- // SaveLog.SetInfo("GetStatment conn NULL", 2);
- conn = ConnectDB();
- }
- ResultSet hjrs = null;
- Statement hjStmt = null;
- try {
- hjStmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, //TYPE_SCROLL_INSENSITIVE
- ResultSet.CONCUR_READ_ONLY);
- hjrs = hjStmt.executeQuery(Sql);
- hjrs.first();
- count = hjrs.getInt(1);
- return count;
- }
- catch (Exception e) {
- SaveLog.SetInfo(Sql + " :" + e, 2);
- return 0;
- }
- finally {
- try {
- if (hjrs != null) {
- hjrs.close();
- }
- if (hjStmt != null) {
- hjStmt.close();
- }
- }
- catch (Exception e) {
- SaveLog.SetInfo(Sql + "close :" + e, 2);
- }
- }
- }
- /**
- *關閉連接
- */
- public void CloseConn() {
- try {
- conn.close();
- conn = null;
- }
- catch (Exception e) {
- }
- finally {
- try {
- if (conn != null) {
- conn.close();
- }
- }
- catch (Exception e) {
- }
- }
- }
- /**
- * 把上傳的文件保存到數據庫的Blob字段中
- * @param instream 輸入的文件流
- * @param table 對應的表名稱
- * @param blobColumn 表中保存文件的Blob字段名稱
- * @param keyColumn where條件字段名
- * @param keyValue where條件字段值
- * @throws java.lang.Exception
- */
- public void updateBlob(String xmlStr, //InputStream instream
- //Connection conn,
- String table,
- String blobColumn,
- String keyColumn,
- String keyValue) throws SQLException, IOException {
- Statement stmt = null;
- ResultSet rs = null;
- oracle.sql.BLOB blob = null;
- sun.misc.BASE64Decoder base64Decoder = new sun.misc.BASE64Decoder();
- byte b[] = base64Decoder.decodeBuffer(xmlStr);
- InputStream instream = new ByteArrayInputStream(b);
- StringBuffer sqlBuffer = new StringBuffer();
- try {
- conn.setAutoCommit(false);
- sqlBuffer.append("select ");
- sqlBuffer.append(blobColumn);
- sqlBuffer.append(" from ");
- sqlBuffer.append(table);
- sqlBuffer.append(" where ");
- sqlBuffer.append(keyColumn);
- sqlBuffer.append("='");
- sqlBuffer.append(keyValue);
- sqlBuffer.append("' for update ");
- stmt = conn.createStatement();
- rs = (ResultSet) stmt.executeQuery(sqlBuffer.toString());
- if (!rs.next()) {
- rs.close();
- stmt.close();
- throw new IllegalArgumentException(
- "no record found for keyValue: '" + keyValue + "'");
- }
- blob = (oracle.sql.BLOB) rs.getBlob(1);
- OutputStream outstream = null;
- outstream = blob.getBinaryOutputStream();
- int bufferSize = ( (oracle.sql.BLOB) blob).getBufferSize();
- byte[] buffer = new byte[bufferSize];
- int bytesRead = -1;
- while ( (bytesRead = instream.read(buffer)) != -1) {
- outstream.write(buffer, 0, bytesRead);
- }
- instream.close();
- outstream.close();
- rs.close();
- stmt.close();
- //System.out.println(bufferSize);
- }
- catch (SQLException e) {
- throw e;
- }
- catch (IOException e) {
- throw e;
- }
- }
- public static void main(String[] args) throws Exception {
- //讀入檢驗XML配置到內存
- System.out.println(MyFunction.DataTimeStr("yyyyMMddHHmmss") + "insert begin!");
- PreparedStatement pstmt = null;
- ResultSet jlRs = null;
- String Sql = "INSERT INTO T_DE_Student (Student_ID,Student_Name,SpellName,UesdName,Class_ID,XBDM_Code,XX_Code,ID_Card,Birthday,MZ_Code,ZZMM_Code,Birth_Code,Birth_Address,Native_Code,IsSingleton,EmigrantCode,IsFloating,Telephone,Status_Code,ResidenceAddress,PostalAddress,PostalCode,Email,HomePageUrl,B_Use,Reason_ID,Reason_Memo,DataSource_ID,Photo,RXFS_Code,RXQ_School,RX_Date,Create_Date,RecordState_ID,HK_Address,JDFS_Code,XSLB_Code,YXX_Code,Number,HealthCode,Nationality) VALUES ('a386b67e-57ec-4765-b6db-01a3523bff4f','陳思園','','','2','0','4','213654789562463','2005-12-21 0:00:00','06','03','120000','','130000','','','','','','','','','','','1','1','轉出','3','','0','','','','','','0','0','0','0','','')";
- Connection conn = null;
- try {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- }
- catch (java.lang.ClassNotFoundException e) {
- }
- try {
- conn = java.sql.DriverManager.getConnection("jdbc:oracle:oci8:@HBRL", "HBRL", "HBRL");
- conn.setAutoCommit(false);
- }
- catch (java.sql.SQLException e) {
- }
- try{
- for (int i = 0; i < 10000; i++) {
- pstmt = conn.prepareStatement(Sql);
- // pstmt.setInt(1,1);
- // pstmt.setString(2,"2");
- // pstmt.setString(3,"3");
- pstmt.executeUpdate();
- pstmt.close();
- }
- conn.commit();
- }
- catch(Exception e) {
- }
- System.out.println(MyFunction.DataTimeStr("yyyyMMddHHmmss")+"insert ok!");
- /*
- System.out.print("Your writing is successful.");
- System.out.println(MyFunction.DataTimeStr("yyyyMMddHHmmss")+"insert ok!");
- JdbcWork ZzJw = new JdbcWork();
- ResultSet rs = ZzJw.SelectSql(
- "select rowid,t_czrkjbxx.* from t_czrkjbxx where rownum <= 100000");
- ResultSetMetaData rsmd = rs.getMetaData();
- System.out.println(MyFunction.DataTimeStr("yyyyMMddHHmmss"));
- StringBuffer a = new StringBuffer();
- String b, c;
- int m = 0;
- for (int j = 0; j < 100; j++) {
- for (int i = 1; i <= rsmd.getColumnCount(); i++) {
- a.append(rsmd.getColumnName(i)).append(rsmd.getColumnTypeName(i)).
- append(rsmd.
- getColumnDisplaySize(i));
- }
- rs.next();
- m = m + 1;
- }
- System.out.println(MyFunction.DataTimeStr("yyyyMMddHHmmss") + " " + m);
- */
- }
- }
- // end class
jdbcwork.java
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.