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();
        String url = urlTem.toString();
            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;
            selectPro = dbConnection.createStatement();//  Statement object definitions
            dbResultSet = selectPro.executeQuery(selectSql);
                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)));
            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);
        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)
            updatePro = dbConnection.createStatement(); //  Statement object definitions
            updatePro.close();// -------------  Close the Statement object
            return true;
        }catch(Exception err){
            System.out.println("  Database operation failed  !");
            System.out.println("Sql = " + sql);
            return false;
    }//end dbUpdate()

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

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

    public static void main(String[] args)
        String dbHost = "";
        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();
        String selectSql = "select "+tableid+","+areaname+" from "+tableName+" order by JCDID";
        ArrayList<String> fieldsList = new ArrayList<String>();
        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  ");
            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)+"";
                System.out.println(areaname + " = " + areaData+";"+tableid+" = " + ad);
            System.out.println("  Total number of records  :"+infoSize);
    }//end main()

分类:Java 时间:2010-07-21 人气:249
blog comments powered by Disqus


  • JAVA processing time - java.sql.Date.java.util.Date in the Date field in the database conversion method 2010-03-29

    1, how java.util.Date into a java.sql.Date? Transformation: java.sql.Date sd; java.util.Date ud; / / initialize the ud such as ud = new java.util.Date (); sd = new java.sql.Date (ud.getTime ()); 2, if you want to insert into the database and the corr

  • The util java connect SqlServer 2010-07-28

    Write a blog that can help them remember certain things. package edu.cric.zl.jdbc; import java.sql.*; public class DBUtils { private String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private String dbUrl = "jdbc:sqlserver://

  • In SQL Server 2005 which, if the host's TCP / IP connection failed. Java.net. ConnectException: Connecti 2011-02-28

    I am using SQL Server 2005 Standard Edition SP2, these are the default, generally do not configure. See below if you need to configure the port. 1, "Start" → "Programs" → "Microsoft SQL Server 2005" → "Configuration Tool

  • Java 摘自网络的一个数据库连接及操作类DB[以SQL Server 2005为例] 2011-02-14

    一共分三个类: 1.数据库连接类DB.java: (注意要改跟数据库相关的 className.url.password.user 之类的数据) import java.sql.*; /** * 描述: 数据库连接类 Copyright * * @author * @version */ public class DB { // static String driver = "com.mysql.jdbc.Driver"; private static final String dri

  • Java基础教程之包(package) 2013-10-01

    这篇文章主要介绍了Java基础教程之包(package),本文详细讲解了包的创建.使用等方法,需要的朋友可以参考下 我们已经写了一些Java程序.之前的每个Java程序都被保存为一个文件,比如Test.java.随后,该程序被编译为Test.class.我们最终使用$java Test来运行程序. 然而,在一个正常的Java项目中,我们往往需要编写不止一个.java程序,最终的Java产品包括了所有的Java程序.因此,Java需要解决组织Java程序的问题.包(package)的目的就是为了更

  • Solution java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob 2010-06-03

    Save the file to the database at the time, reported java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob Solutions, see red OutputStream out = rs.getBlob (1). SetBinaryStream (1L);

  • Solve the HSQL in java.lang.NoClassDefFoundError: java / sql / Savepoint error 2011-07-16

    Solve the HSQL in java.lang.NoClassDefFoundError: java / sql / Savepoint error Double-click the directory in hsql managedb.bat when java.lang.NoClassDefFoundError: java / sql / Savepoint error. Check the environment variable path is ora92 original pr

  • Java class TestStoreProcedure.java test the stored procedure 2011-06-01

    Use the following Java classes TestStoreProcedure.java test stored procedures. package test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import oracle.jdbc.OracleTypes; /

  • java.lang.OutOfMemoryError: Java heap space 的解决方法 2015-01-23

    关键字: tomcat outofmemoryerror permgen space java heap space 最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植到oracle,首先把jdbc 的连接指向mysql,打包放到tomcat里面,可以跑起来,没有问题,可是当把jdbc连接指向oracle的时候,tomcat就连续抛 java.lang.OutOfMemoryError的错误,上网google了一下,了解了一下tomcat的运行机制,也解决了问题,share出来,以备

  • Java学习资料-Java对象序列化ObjectOut(In)putStream 2015-03-05

    Java中ObjectInputStream 与 ObjectOutputStream这两个包装类可用于输入流中读取对象类数据和将对象类型的数据写入到底层输入流 .ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口.需要注意的是:对象中的 transient 和 static 类型的成员变量不会被读取和写入 . 具体代码示例: O bjectFileConvert.java package michael.io;

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.062 (s). 13 q(s)