posts - 0, comments - 0, trackbacks - 0, articles - 23
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
package ConnectionTest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ContactTest {       
        private Connection conn = null;
        private int flag;
       
        static final String LINE_SEPARATOR = System.getProperty("line.separator");
       
        public void getConnection(){
                try{
                        String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
                        String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";                                       
                        String USER = "PBPUBLIC";
                        String PASSWORD = "PBPUBLIC";
               
                        Class.forName(JDBC_DRIVER).newInstance();
                        conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
                        System.out.println("connection success");
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
       
        public void closeConnection() throws SQLException{
                if(this.conn != null){
                        conn.close();
                }
        }
       
        public void getResult(int flag,Collection col) throws IOException{
                String name = null;
                if(flag == 0){
                        System.out.println("输出查询信息");
                        name = "select.csv";
                        WriterOut(name,col);
                }else if(flag == 1){
                        System.out.println("输出插入信息");
                        name = "insert.csv";
                        WriterOut(name,col);
                }else if(flag == 2){
                        System.out.println("输出修改信息");
                        name = "update.csv";
                        WriterOut(name,col);
                }else if(flag == 3){
                        System.out.println("输出删除信息");
                        name = "delete.csv";
                        WriterOut(name,col);
                }else{
                        System.out.println("标志码输入错误");
                }
        }
       
        public void WriterOut(String name,Collection list) throws IOException{
                String path = "d:/"+name;
       
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
        int count = list.size();
        String csvData[] = new String[count];
       
        Iterator it = list.iterator();
       
        int i = 0;
        while(it.hasNext()){
                String temp = (String)it.next();
                System.out.println(temp);
                csvData[i] = temp;
                i++;
        }
       
        for(int j = 0;j < csvData.length;j++)
                {
                        writer.write(csvData[j] + LINE_SEPARATOR);
                       
                }
        writer.close();
      
        }
       
        public void SelectDB(){
                try{
                        getConnection();
                        Statement stmt  = this.conn.createStatement();
                        String SQL = "select id ,name ,age ,address  from test";
                        ResultSet rs = stmt.executeQuery(SQL);

                        Collection col = new ArrayList();
                       
                        ResultSetMetaData ss = rs.getMetaData();       
                        int count = ss.getColumnCount();

                        String temp = "";
                        for(int j = 1; j <= count; j++){
                                System.out.print(ss.getColumnName(j)+"        ");
                                if(j == count){
                                        temp = temp + ss.getColumnName(j);
                                }else{
                                        temp = temp + ss.getColumnName(j)+",";
                                }
                        }
                        col.add(temp);
        
                        System.out.println(" ");
                       
                        while(rs.next()){       
                                temp = "";
                                for (int i = 1; i <= count;i++){
                                        System.out.print(rs.getString(i)+" ");
                                        if(i == count){
                                                temp = temp + "\""+ rs.getString(i) + "\"";
                                        }else{
                                                temp = temp + "\""+ rs.getString(i) + "\",";
                                        }       
                                }
                                col.add(temp);   //
                                System.out.println(" ");
                        }
                        getResult(0,col);
                        closeConnection();
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
       
        public void InsertDB(){
                try{
                        getConnection();
                        String SQL = "insert into test values('0011','sandy','25','shanghai')";
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                       
                        ResultSet rs = pstmt.executeQuery();
                        conn.commit();
                       
                        SQL = "select id ,name ,age ,address  from test";
                        Statement stmt = this.conn.createStatement();
                        rs = stmt.executeQuery(SQL);
                        ResultSetMetaData ss = rs.getMetaData();       
                        int count = ss.getColumnCount();

                        Collection col = new ArrayList();
                       
            String temp = "";
                        for(int j = 1; j <= count; j++){
                                System.out.print(ss.getColumnName(j)+"        ");
                                if(j == count){
                                        temp = temp + ss.getColumnName(j);
                                }else{
                                        temp = temp + ss.getColumnName(j)+",";
                                }
                        }
                        col.add(temp);
                        System.out.println(" ");
           
                        while(rs.next()){       
                                temp = "";
                                for (int i = 1; i <= count;i++){
                                        System.out.print(rs.getString(i)+" ");
                                        if(i == count){
                                                temp = temp + "\""+ rs.getString(i) + "\"";
                                        }else{
                                                temp = temp + "\""+ rs.getString(i) + "\",";
                                        }
                                }
                                col.add(temp);
                                System.out.println(" ");
                        }
                        getResult(1,col);
                        closeConnection();
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
       
        public void UpdateDB(){
                try{
                        getConnection();
                        String SQL = "update test set address = '上海'";
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                       
                        ResultSet rs = pstmt.executeQuery();
                        conn.commit();
                       
                        SQL = "select id ,name ,age ,address  from test";
                        Statement stmt = this.conn.createStatement();
                        rs = stmt.executeQuery(SQL);
                        ResultSetMetaData ss = rs.getMetaData();       
                        int count = ss.getColumnCount();
                       
                        Collection col = new ArrayList();
                       
            String temp = "";
                       
                        for(int j = 1; j <= count; j++){
                                System.out.print(ss.getColumnName(j)+"        ");
                                if(j == count){
                                        temp = temp + ss.getColumnName(j);
                                }else{
                                        temp = temp + ss.getColumnName(j)+",";
                                }
                        }
                        col.add(temp);

                        System.out.println(" ");
                       
                        while(rs.next()){       
                                temp = "";
                                for (int i = 1; i <= count;i++){
                                        System.out.print(rs.getString(i)+" ");
                                        if(i == count){
                                                temp = temp + "\""+ rs.getString(i) + "\"";
                                        }else{
                                                temp = temp + "\""+ rs.getString(i) + "\",";
                                        }
                                }
                                col.add(temp);
                                System.out.println(" ");
                        }
                        getResult(2,col);
                        closeConnection();
                }catch(Exception e){
                        e.printStackTrace();
                }
               
        }
       
        public void DeleteDB(){
                try{
                        getConnection();
                        String SQL = "delete from test where id = '0008'";
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                       
                        ResultSet rs = pstmt.executeQuery();       
                        conn.commit();
                       
                        SQL = "select id ,name ,age ,address  from test";
                        Statement stmt = this.conn.createStatement();
                        rs = stmt.executeQuery(SQL);
                        ResultSetMetaData ss = rs.getMetaData();       
                        int count = ss.getColumnCount();
                       
                        Collection col = new ArrayList();
                       
            String temp = "";
                       
                        for(int j = 1; j <= count; j++){
                                System.out.print(ss.getColumnName(j)+"        ");
                                if(j == count){
                                        temp = temp + ss.getColumnName(j);
                                }else{
                                        temp = temp + ss.getColumnName(j)+",";
                                }
                        }
                        col.add(temp);
                        System.out.println(" ");
                       
                        while(rs.next()){       
                                temp = "";
                                for (int i = 1; i <= count;i++){
                                        System.out.print(rs.getString(i)+" ");
                                        if(i == count){
                                                temp = temp + "\""+ rs.getString(i) + "\"";
                                        }else{
                                                temp = temp + "\""+ rs.getString(i) + "\",";
                                        }
                                }
                                col.add(temp);
                                System.out.println(" ");
                        }
                        getResult(3,col);
                        closeConnection();
                }catch(Exception e){
                        e.printStackTrace();
                }
               
        }
       
        public void excute(int tag){
                if(tag == 0){
                        System.out.println("进行查询操作");
                        SelectDB();
                }else if(tag == 1){
                        System.out.println("进行添加操作");
                        InsertDB();
                }else if(tag == 2){
                        System.out.println("进行修改操作");
                        UpdateDB();
                }else if(tag == 3){
                        System.out.println("进行删除操作");
                        DeleteDB();
                }else{
                        System.out.println("标志码输入错误");
                }
        }
       
        public static void main(String[] args)
        {
                ContactTest  ct = new ContactTest ();
                ct.flag = 3;  //0-查询;1-添加;2-修改;3-删除
                ct.excute(ct.flag);
        }
       
}