jdbc connect to the database (change)

package org.craf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FilterData {
        private Connection dbConnection = null;
    private Statement selectPro = null; //  Used to select operation
    private Statement updatePro = null; //  For update operations
    private ResultSet dbResultSet = null; //  Operating select the result set  

    private String driverName;//  MySql driver class declaration
    private String dbHost;
    private String dbPort;
    private String dbName;
    private String dbUserName;
    private String dbPassword;
    private String enCoding; 

    /** *//**
     *   DbConnection object instantiation
     * @param host   Database Host  (IP)
     * @param port   Database port
     * @param dName   Database Name
     * @param uName   Username
     * @param password   Password
     */
    public FilterData(String host, String port, String dName, String uName, String password)
    {
        driverName = "com.sybase.jdbc3.jdbc.SybDriver";
        dbHost = host;
        dbPort = port;
        dbName = dName;
        dbUserName = uName;
        dbPassword = password;
        enCoding = "?Charset";
    }//end DbConnection()

    /** *//**
     *   Connect to the database
     * @return  Connection successful return true, the connection fails to return  false
     */
    public boolean dbConnection()
    {
        StringBuilder urlTem = new StringBuilder();
        urlTem.append("jdbc:sybase:Tds:");
        urlTem.append(dbHost);
        urlTem.append(":");
        urlTem.append(dbPort);
        urlTem.append("/");
        urlTem.append(dbName);
        urlTem.append(enCoding);
        String url = urlTem.toString();
        try
        {
            Class.forName(driverName).newInstance();
            dbConnection = DriverManager.getConnection(url, dbUserName, dbPassword);
            return true;
        }catch(Exception e){
            System.err.println("  Database connection failed  !");
            System.out.println("url = " + url);
            e.printStackTrace(); //  Detailed error message received
            return false;
        }
    }// end dbConnection()

    /** *//**
     *   Select the implementation of special operations  ,  Note  :  SelectSql the field and in the  fields  In number. Name to be consistent
     * @param selectSql   For the implementation of the select statement
     * @param fields   To select the field
     * @return   Map of containing  ArrayList,  A record form a  Map
     */
    public ArrayList dbSelect(String selectSql, ArrayList fields)
    {
        ArrayList<Map> selectResult = new ArrayList<Map>();
        Map<String, String> recordInfo;
        try{
            selectPro = dbConnection.createStatement();//  Statement object definitions
            dbResultSet = selectPro.executeQuery(selectSql);
            while(dbResultSet.next()){
                recordInfo = new HashMap<String, String>();
                for(int i = 0; i<fields.size(); ++i)
                    recordInfo.put((String)fields.get(i), dbResultSet.getString((String)fields.get(i)));
                selectResult.add(recordInfo);
            }
            dbResultSet.close(); //  Disconnect the result set
            selectPro.close(); //  Disconnect the Statement object
        }catch(Exception e){
            System.out.println("  Select operation failed  ");
            System.out.println("Sql = " + selectSql);
            e.printStackTrace();
        }
        return selectResult;
    }//end dbSelect()

    /** *//**
     *   Performed on the database update, delete or  insert  Operation
     * @param sql   SQL statement to perform the operation
     * @return   The implementation of successful return true, failure to return  false
     */
    public boolean dbUpdate(String sql)
    {
        try
        {
            updatePro = dbConnection.createStatement(); //  Statement object definitions
            updatePro.executeUpdate(sql);
            updatePro.close();// -------------  Close the Statement object
            return true;
        }catch(Exception err){
            System.out.println("  Database operation failed  !");
            System.out.println("Sql = " + sql);
            err.printStackTrace();
            return false;
        }
    }//end dbUpdate()

    /** *//**
     *   Close the database connection
     * @return   Successful return true, failure to return  false
     */
    public boolean closeDatabase()
    {
        try{
            if(dbConnection != null)
                dbConnection.close();
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }//end closeDatabase()

    public String filterData(String area){
        String areas[] = area.split("-");
        String resultData = "";
        if(areas.length == 3){
                resultData = areas[0]+"-"+areas[2];
        }else{
                resultData = area;
        }
        return resultData;
    }

    public static void main(String[] args)
    {
        String dbHost = "192.168.0.57";
        String dbPort = "5000";
        String dbName = "ssjg";
        String dbUserName = "sa";
        String dbPassword = "123456";
        String tableName = "BASEDATA_50000011";
        String tableid = "BASEDATAID";
        String areaname = "JCDQNAME";
        FilterData conn = new FilterData(dbHost, dbPort, dbName, dbUserName, dbPassword);
        boolean bool = conn.dbConnection();
        if(!bool)
            return;
        String selectSql = "select "+tableid+","+areaname+" from "+tableName+" order by JCDID";
        ArrayList<String> fieldsList = new ArrayList<String>();
        fieldsList.add(tableid);
        fieldsList.add(areaname);
        ArrayList<Map> userInfoList = conn.dbSelect(selectSql, fieldsList);
        int infoSize = userInfoList.size();
        String userName;
        String ad;
        if(infoSize == 0)
            System.out.println("  No data selected eligible  ");
        else{
            for(int i = 0; i < infoSize; ++i){
                userName = (String)userInfoList.get(i).get(areaname);
                ad = (String)(((Map)userInfoList.get(i)).get(tableid));
                String areaData = conn.filterData(userName);
                String updateSql = "update "+tableName+" set "+areaname+" = '" + areaData + "' where "+tableid+" ="+new Integer(ad)+"";
                conn.dbUpdate(updateSql);
                System.out.println(areaname + " = " + areaData+";"+tableid+" = " + ad);
            }
            System.out.println("  Total number of records  :"+infoSize);
        }
        conn.closeDatabase();
    }//end main()

}
分类:Java 时间:2010-07-21 人气:227
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.706 (s). 12 q(s)