java语言用jdbc操作sql2000数据库

package com.cnnct.jxmz.shsw.hygl.lhdj;
/**
 *  本类封装所有对数据库的操作
 *  @author songxf
 *  @version 1.0    2008.02
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.*;
import java.util.*;
import com.cnnct.exception.*;
import com.cnnct.util.*;
import com.lbs.sieaf.exception.*;
import com.lbs.sieaf.util.GlobalNames;

 

public class DB2Util{

        private static DB2Util db = null;

        private static Connection conn;

        private static PreparedStatement pStatement;


        // private ResultSet m_rs;

        private static String driver_class = null;

        private static String url = null;

        private static String username = null;

        private static String password = null;

 

        public DB2Util(){

                try {
                        Class.forName(driver_class);
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }
        static {
                try {
                        driver_class = GlobalNames.DB_JDBC_DRIVER;
                        url = GlobalNames.DB_JDBC_URL;
                        username = GlobalNames.DB_USERNAME;
                        password = GlobalNames.DB_PASSWORD;

                } catch (Exception ex) {
                       ex.printStackTrace();
                }
        }

        public static DB2Util getInstance() {
                if (db == null) {
                        db = new DB2Util();
                }
                return db;
        }
        /**
   * This method is used to update the current object in DB
   * @see com.lbs.sieaf.dao.DAO#doUpdate(Connection, Object)
   * @param conn java.sql.Connection
   * @param obj an Object being to update
   * @throws SQLException
   */
  public void doUpdate( Object obj) throws SQLException {

    LhdjDTO dto = (LhdjDTO) obj;
    Void uid = new UID();
    uid.toString();
    StringBuffer sbSQL = new StringBuffer();
    sbSQL.append("UPDATE MZ_HY_LHDJ SET ");
    sbSQL.append(" XM = ? , "); 
    try {
      conn=DriverManager.getConnection(url, username, password);
      pStatement = conn.prepareStatement(sbSQL.toString());
      pStatement.clearParameters();

      int i = 1;
      pStatement.setString(i++, dto.getXm());       try {
        pStatement.executeUpdate();
      }
      catch (SQLException se) {
        throw se;
      }
    }
    finally {
      close();
      sbSQL = null;

    }
  }

   public void doStore( Object obj) throws SQLException {

    LhdjDTO dto = (LhdjDTO) obj;

    StringBuffer sbSQL = new StringBuffer();
    sbSQL.append(" INSERT INTO MZ_HY_LHDJ (");
    sbSQL.append(" XM , ");      sbSQL.append(" ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?)");//*******************


    try {
      conn=DriverManager.getConnection(url, username, password);
      pStatement = conn.prepareStatement(sbSQL.toString());
      pStatement.clearParameters();

      int i = 1;
      pStatement.setString(i++, dto.getXm());  

      try {
        pStatement.executeUpdate();
      }
      catch (SQLException se) {
        throw se;
      }
    }
    finally {
     close();

      sbSQL = null;

    }
  }


  public void doDelete( Object obj) throws SQLException {

    LhdjDTO dto = (LhdjDTO) obj;

    StringBuffer sbSQL = new StringBuffer();
    sbSQL.append(" DELETE FROM MZ_HY_LHDJ ");
    sbSQL.append(" WHERE 1= 1");
    sbSQL.append(" AND LHDJXH= ? "); 
    try {
      conn=DriverManager.getConnection(url, username, password);
      pStatement = conn.prepareStatement(sbSQL.toString());
      pStatement.clearParameters();

      pStatement.setString(1, dto.getLhdjxh()); 

      try {
        pStatement.executeUpdate();
      }
      catch (SQLException se) {
        throw se;
      }
    }
    finally {
      close();

      sbSQL = null;

    }
  }
  public  void close() throws SQLException {
                  try {
                          if (pStatement != null)
                                  pStatement.close();
                          if (conn != null)
                                  conn.close();
                  } catch (SQLException e) {
                          throw new SQLException("关闭数据库连接出错!");
                  }
          }

 

        public static void main(String []a){
//  String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//  String url="jdbc:microsoft:sqlserver://localhost:1433;databasename=tempdb";
//  String driver="oracle.jdbc.driver.OracleDriver";
//  String url="jdbc:oracle:thin:@172.16.200.3:1521:wxcj";
                driver_class = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                url = "jdbc:microsoft:sqlserver://192.168.3.5:1433;databasename=DB_CustomSMS";
                Statement m_stmt;
                try {

                        Class.forName(driver_class);
                        conn = DriverManager.getConnection(url, "CustomSms", "SqlMsde@InfoxEie2000");
                        m_stmt = conn.createStatement();
                        m_stmt.execute("select * from tbl_SMReceived");
                        //m_stmt.executeUpdate("insert into cgsj(content,msisdn,longcode) values('你好,这里是测试','13116769533','19')");
//   ResultSet resultSet = m_stmt.executeQuery("select t.*, t.rowid from yw_cjydlrz t where logid>39015");
//   if (resultSet != null) {
//    List list = new ArrayList();
//    ResultSetMetaData resultMetaData = resultSet.getMetaData();
//    int fieldCount = resultMetaData.getColumnCount();
//    while (resultSet.next()) {
//     HashMap hm = new HashMap();
//     for (int i = 1; i <= fieldCount; i++) {
//      String fieldName = resultMetaData.getColumnName(i);
//      Object fieldValue = resultSet.getObject(fieldName);
//      if(resultMetaData.getColumnType(i) == Types.VARCHAR && fieldValue == null){
//       fieldValue = "";
//      }
//      if(fieldValue instanceof java.util.Date){
//       System.out.println(DateUtil.formatDate((Date)resultSet.getObject(fieldName), ""));
//       System.out.println(DateUtil.formatDate((Date)resultSet.getTimestamp(fieldName), ""));
//      }
//      hm.put(fieldName, fieldValue);
//     }
//     list.add(hm);
//    }
//    resultSet.close();
//   }
                        if (m_stmt != null)
                                m_stmt.close();
                        if (conn != null)
                                conn.close();

                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                //m_stmt.executeUpdate(sql);
                //close();
        }

 

}
 

你可能感兴趣的:(java语言用jdbc操作sql2000数据库)