- /**
- * 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
最新推荐文章于 2024-08-15 11:29:03 发布