以下问题是这一期间回答waterlily 关于java的一些整理,发现对大家都很有作用,现在将这些详细讲解的程序单独列出来供大家参考,学习,讨论
------------------------------------------------------------
1.使用jdbc连接数据库--通过jdbc连接数据库,将SQL语句运行结果打印到控制台
 package ConnectionTest;
package ConnectionTest;

 import java.sql.Connection;
import java.sql.Connection;
 import java.sql.DriverManager;
import java.sql.DriverManager;
 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
import java.sql.ResultSetMetaData;
 import java.sql.Statement;
import java.sql.Statement;


 public class ContactTest
public class ContactTest  {
{        
 private Connection conn = null;
        private Connection conn = null;
 
        

 public void getConnection()
        public void getConnection() {
{

 try
                try {
{
 String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
                        String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
 String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";
                        String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";                                        
 String USER = "PBPUBLIC";
                        String USER = "PBPUBLIC";
 String PASSWORD = "PBPUBLIC";
                        String PASSWORD = "PBPUBLIC";
 
                        
 Class.forName(JDBC_DRIVER).newInstance();
                        Class.forName(JDBC_DRIVER).newInstance();
 conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
                        conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
 System.out.println("connection success");
                        System.out.println("connection success");

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

 public void excute()
        public void excute() {
{
 
                

 try
                try {
{
 Statement stmt  = this.conn.createStatement();
                        Statement stmt  = this.conn.createStatement();
 String SQL = "select id ,name ,age ,address  from test";
                        String SQL = "select id ,name ,age ,address  from test";
 ResultSet rs = stmt.executeQuery(SQL);
                        ResultSet rs = stmt.executeQuery(SQL);

 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();
 
                                
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();


 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");
 }
                        }
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");
 }
                                }
 System.out.println(" ");
                                System.out.println(" ");
 }
                        } 
 rs.close();
                        rs.close();
 stmt.close();
                        stmt.close();
 this.conn.close();
                        this.conn.close();                       

 }catch(Exception e)
                }catch(Exception e) {
{
 e.printStackTrace();
                        e.printStackTrace();
 }
                }
 
                
 }
        }
 
        
 public static void main(String[] args)
        public static void main(String[] args) 

 
         {
{
 ContactTest  ct = new ContactTest ();
                ContactTest  ct = new ContactTest ();
 ct.getConnection();
                ct.getConnection();
 ct.excute();
                ct.excute();
 }
        }
 
        
 }
}

 package ConnectionTest;
package ConnectionTest;

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


 public class ContactTest
public class ContactTest  {
{        
 private Connection conn = null;
        private Connection conn = null;
 private int flag;
        private int flag;
 
        
 
        
 
        

 public void getConnection()
        public void getConnection() {
{

 try
                try {
{
 String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
                        String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
 String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";
                        String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";                                        
 String USER = "PBPUBLIC";
                        String USER = "PBPUBLIC";
 String PASSWORD = "PBPUBLIC";
                        String PASSWORD = "PBPUBLIC";
 
                        
 Class.forName(JDBC_DRIVER).newInstance();
                        Class.forName(JDBC_DRIVER).newInstance();
 conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
                        conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
 System.out.println("connection success");
                        System.out.println("connection success");

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

 public void closeConnection() throws SQLException
        public void closeConnection() throws SQLException {
{

 if(this.conn != null)
                if(this.conn != null) {
{
 conn.close();
                        conn.close();
 }
                }
 }
        }
 
        

 public void getResult(int flag,Collection col) throws IOException
        public void getResult(int flag,Collection col) throws IOException {
{
 String name = null;
                String name = null;

 if(flag == 0)
                if(flag == 0) {
{
 System.out.println("输出查询信息");
                        System.out.println("输出查询信息");
 name = "select.txt";
                        name = "select.txt";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 1)
                }else if(flag == 1) {
{
 System.out.println("输出插入信息");
                        System.out.println("输出插入信息");
 name = "insert.txt";
                        name = "insert.txt";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 2)
                }else if(flag == 2) {
{
 System.out.println("输出修改信息");
                        System.out.println("输出修改信息");
 name = "update.txt";
                        name = "update.txt";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 3)
                }else if(flag == 3) {
{
 System.out.println("输出删除信息");
                        System.out.println("输出删除信息");
 name = "delete.txt";
                        name = "delete.txt";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        

 public void WriterOut(String name,Collection list) throws IOException
        public void WriterOut(String name,Collection list) throws IOException {
{
 String path = "d:/"+name;
                String path = "d:/"+name;
 File newFile = new File(path);  //这里注意你的输入文件的路径
                File newFile = new File(path);  //这里注意你的输入文件的路径
 BufferedWriter wirter = new BufferedWriter(new FileWriter(newFile));
                BufferedWriter wirter = new BufferedWriter(new FileWriter(newFile));
 
                
 Iterator it = list.iterator();
                Iterator it = list.iterator();

 while(it.hasNext())
                while(it.hasNext()) {
{
 String temp = (String)it.next();
                        String temp = (String)it.next();
 System.out.println(temp);
                        System.out.println(temp);
 wirter.newLine();
                        wirter.newLine();
 wirter.write(temp);
                        wirter.write(temp);
 wirter.flush();
                        wirter.flush();
 }
                }
 wirter.close();
                wirter.close();
 
                
 }
        }
 
        

 public void SelectDB()
        public void SelectDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 Statement stmt  = this.conn.createStatement();
                        Statement stmt  = this.conn.createStatement();
 String SQL = "select id ,name ,age ,address  from test";
                        String SQL = "select id ,name ,age ,address  from test";
 ResultSet rs = stmt.executeQuery(SQL);
                        ResultSet rs = stmt.executeQuery(SQL);
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList(); 
 
                        
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();

 String temp = "";
                        String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");
 temp = temp + ss.getColumnName(j)+"              ";
                                temp = temp + ss.getColumnName(j)+"              ";
 }
                        }
 col.add(temp);
                        col.add(temp);
 
                        
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");
 temp = temp + rs.getString(i)+"       ";
                                        temp = temp + rs.getString(i)+"       ";                
 }
                                }
 col.add(temp);   //
                                col.add(temp);   //
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(0,col);
                        getResult(0,col);
 rs.close();
                        rs.close();
 closeConnection();
                        closeConnection();

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

 public void InsertDB()
        public void InsertDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "insert into test values('0011','sandy','25','shanghai')";
                        String SQL = "insert into test values('0011','sandy','25','shanghai')";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 this.conn.commit();
                        this.conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
                        String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");
 temp = temp + ss.getColumnName(j)+"              ";
                                temp = temp + ss.getColumnName(j)+"              ";
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");
 temp = temp + rs.getString(i)+"       ";
                                        temp = temp + rs.getString(i)+"       ";
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 
                        
 getResult(1,col);
                        getResult(1,col);
 rs.close();
                        rs.close();
 closeConnection();
                        closeConnection();

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

 public void UpdateDB()
        public void UpdateDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "update test set address = '上海'";
                        String SQL = "update test set address = '上海'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 this.conn.commit();
                        this.conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
                        String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");
 temp = temp + ss.getColumnName(j)+"              ";
                                temp = temp + ss.getColumnName(j)+"              ";
 }
                        }
 col.add(temp);
                        col.add(temp);
 
                        
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");
 temp = temp + rs.getString(i)+"       ";
                                        temp = temp + rs.getString(i)+"       ";
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 
                         
 getResult(2,col);
                        getResult(2,col);
 rs.close();
                        rs.close();
 closeConnection();
                        closeConnection();

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

 public void DeleteDB()
        public void DeleteDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "delete from test where id = '0008'";
                        String SQL = "delete from test where id = '0008'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();  
 this.conn.commit();
                        this.conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
                        String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");
 temp = temp + ss.getColumnName(j)+"              ";
                                temp = temp + ss.getColumnName(j)+"              ";
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");
 temp = temp + rs.getString(i)+"       ";
                                        temp = temp + rs.getString(i)+"       ";
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 
                        
 getResult(3,col);
                        getResult(3,col);
 rs.close();
                        rs.close();
 closeConnection();
                        closeConnection();

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

 public void excute(int tag)
        public void excute(int tag) {
{

 if(tag == 0)
                if(tag == 0) {
{
 System.out.println("进行查询操作");
                        System.out.println("进行查询操作");
 SelectDB();
                        SelectDB();

 }else if(tag == 1)
                }else if(tag == 1) {
{
 System.out.println("进行添加操作");
                        System.out.println("进行添加操作");
 InsertDB();
                        InsertDB();

 }else if(tag == 2)
                }else if(tag == 2) {
{
 System.out.println("进行修改操作");
                        System.out.println("进行修改操作");
 UpdateDB();
                        UpdateDB();

 }else if(tag == 3)
                }else if(tag == 3) {
{
 System.out.println("进行删除操作");
                        System.out.println("进行删除操作");
 DeleteDB();
                        DeleteDB();

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        
 public static void main(String[] args)
        public static void main(String[] args) 

 
         {
{
 ContactTest  ct = new ContactTest ();
                ContactTest  ct = new ContactTest ();
 ct.flag = 3;  //0-查询;1-添加;2-修改;3-删除
                ct.flag = 3;  //0-查询;1-添加;2-修改;3-删除
 ct.excute(ct.flag);
                ct.excute(ct.flag);
 }
        }
 
        
 }
}


 package ConnectionTest;
package ConnectionTest;

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


 public class ContactTest
public class ContactTest  {
{        
 private Connection conn = null;
        private Connection conn = null;
 private int flag;
        private int flag;
 
        
 static final String LINE_SEPARATOR = System.getProperty("line.separator");
        static final String LINE_SEPARATOR = System.getProperty("line.separator");
 
        

 public void getConnection()
        public void getConnection() {
{

 try
                try {
{
 String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
                        String JDBC_URL = "jdbc:pointbase:server://localhost:9092/test";
 String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";
                        String JDBC_DRIVER="com.pointbase.jdbc.jdbcUniversalDriver";                                        
 String USER = "PBPUBLIC";
                        String USER = "PBPUBLIC";
 String PASSWORD = "PBPUBLIC";
                        String PASSWORD = "PBPUBLIC";
 
                
 Class.forName(JDBC_DRIVER).newInstance();
                        Class.forName(JDBC_DRIVER).newInstance();
 conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
                        conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
 System.out.println("connection success");
                        System.out.println("connection success");

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

 public void closeConnection() throws SQLException
        public void closeConnection() throws SQLException {
{

 if(this.conn != null)
                if(this.conn != null) {
{
 conn.close();
                        conn.close();
 }
                }
 }
        }
 
        

 public void getResult(int flag,Collection col) throws IOException
        public void getResult(int flag,Collection col) throws IOException {
{
 String name = null;
                String name = null;

 if(flag == 0)
                if(flag == 0) {
{
 System.out.println("输出查询信息");
                        System.out.println("输出查询信息");
 name = "select.csv";
                        name = "select.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 1)
                }else if(flag == 1) {
{
 System.out.println("输出插入信息");
                        System.out.println("输出插入信息");
 name = "insert.csv";
                        name = "insert.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 2)
                }else if(flag == 2) {
{
 System.out.println("输出修改信息");
                        System.out.println("输出修改信息");
 name = "update.csv";
                        name = "update.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 3)
                }else if(flag == 3) {
{
 System.out.println("输出删除信息");
                        System.out.println("输出删除信息");
 name = "delete.csv";
                        name = "delete.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        

 public void WriterOut(String name,Collection list) throws IOException
        public void WriterOut(String name,Collection list) throws IOException {
{
 String path = "d:/"+name;
                String path = "d:/"+name;
 
        
 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
 int count = list.size();
        int count = list.size();
 String csvData[] = new String[count];
        String csvData[] = new String[count];
 
        
 Iterator it = list.iterator();
        Iterator it = list.iterator();
 
        
 int i = 0;
        int i = 0;

 while(it.hasNext())
        while(it.hasNext()) {
{
 String temp = (String)it.next();
                String temp = (String)it.next();
 System.out.println(temp);
                System.out.println(temp);
 csvData[i] = temp;
                csvData[i] = temp;
 i++;
                i++;
 }
        }
 
        
 for(int j = 0;j < csvData.length;j++)
        for(int j = 0;j < csvData.length;j++)

 
                 {
{
 writer.write(csvData[j] + LINE_SEPARATOR);
                        writer.write(csvData[j] + LINE_SEPARATOR);
 
                        
 }
                }
 writer.close();
        writer.close();
 
       
 }
        }
 
        

 public void SelectDB()
        public void SelectDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 Statement stmt  = this.conn.createStatement();
                        Statement stmt  = this.conn.createStatement();
 String SQL = "select id ,name ,age ,address  from test";
                        String SQL = "select id ,name ,age ,address  from test";
 ResultSet rs = stmt.executeQuery(SQL);
                        ResultSet rs = stmt.executeQuery(SQL);

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

 String temp = "";
                        String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 
         
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }        
 }
                                }
 col.add(temp);   //
                                col.add(temp);   //
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(0,col);
                        getResult(0,col);
 closeConnection();
                        closeConnection();

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

 public void InsertDB()
        public void InsertDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "insert into test values('0011','sandy','25','shanghai')";
                        String SQL = "insert into test values('0011','sandy','25','shanghai')";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();

 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
            

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(1,col);
                        getResult(1,col);
 closeConnection();
                        closeConnection();

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

 public void UpdateDB()
        public void UpdateDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "update test set address = '上海'";
                        String SQL = "update test set address = '上海'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);

 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(2,col);
                        getResult(2,col);
 closeConnection();
                        closeConnection();

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

 public void DeleteDB()
        public void DeleteDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "delete from test where id = '0008'";
                        String SQL = "delete from test where id = '0008'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();        
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(3,col);
                        getResult(3,col);
 closeConnection();
                        closeConnection();

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

 public void excute(int tag)
        public void excute(int tag) {
{

 if(tag == 0)
                if(tag == 0) {
{
 System.out.println("进行查询操作");
                        System.out.println("进行查询操作");
 SelectDB();
                        SelectDB();

 }else if(tag == 1)
                }else if(tag == 1) {
{
 System.out.println("进行添加操作");
                        System.out.println("进行添加操作");
 InsertDB();
                        InsertDB();

 }else if(tag == 2)
                }else if(tag == 2) {
{
 System.out.println("进行修改操作");
                        System.out.println("进行修改操作");
 UpdateDB();
                        UpdateDB();

 }else if(tag == 3)
                }else if(tag == 3) {
{
 System.out.println("进行删除操作");
                        System.out.println("进行删除操作");
 DeleteDB();
                        DeleteDB();

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        
 public static void main(String[] args)
        public static void main(String[] args) 

 
         {
{
 ContactTest  ct = new ContactTest ();
                ContactTest  ct = new ContactTest ();
 ct.flag = 3;  //0-查询;1-添加;2-修改;3-删除
                ct.flag = 3;  //0-查询;1-添加;2-修改;3-删除
 ct.excute(ct.flag);
                ct.excute(ct.flag);
 }
        }
 
        
 }
}

 package ConnectionTest;
package ConnectionTest;

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


 public class ContactTest
public class ContactTest  {
{        
 private Connection conn = null;
        private Connection conn = null;
 private int flag;
        private int flag;
 
        
 static final String LINE_SEPARATOR = System.getProperty("line.separator");
        static final String LINE_SEPARATOR = System.getProperty("line.separator");
 
        

 public void getConnection()
        public void getConnection() {
{

 try
                try {
{

 /**//*
                        /**//*
 * 修改了程序,通过从配置文件中取得数据库信息
                         * 修改了程序,通过从配置文件中取得数据库信息
 */
                         */
 File file = new File("./src/test/ConnectionTest/config.properties");  //确定你的目录路径
                         File file = new File("./src/test/ConnectionTest/config.properties");  //确定你的目录路径
 Properties props = new Properties();
                 Properties props = new Properties();

 props.load(new FileInputStream(file));
                 props.load(new FileInputStream(file));
 
                        
 
                        
 String JDBC_URL = props.getProperty("db_url");
                        String JDBC_URL = props.getProperty("db_url");
 String JDBC_DRIVER= props.getProperty("db_driver");;
                        String JDBC_DRIVER= props.getProperty("db_driver");;                                        
 String USER = props.getProperty("db_user");;
                        String USER = props.getProperty("db_user");;
 String PASSWORD = props.getProperty("db_password");;
                        String PASSWORD = props.getProperty("db_password");;
 
                
 Class.forName(JDBC_DRIVER).newInstance();
                        Class.forName(JDBC_DRIVER).newInstance();
 conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
                        conn = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
 System.out.println("connection success");
                        System.out.println("connection success");

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

 public void closeConnection() throws SQLException
        public void closeConnection() throws SQLException {
{

 if(this.conn != null)
                if(this.conn != null) {
{
 conn.close();
                        conn.close();
 }
                }
 }
        }
 
        

 public void getResult(int flag,Collection col) throws IOException
        public void getResult(int flag,Collection col) throws IOException {
{
 String name = null;
                String name = null;

 if(flag == 0)
                if(flag == 0) {
{
 System.out.println("输出查询信息");
                        System.out.println("输出查询信息");
 name = "select.csv";
                        name = "select.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 1)
                }else if(flag == 1) {
{
 System.out.println("输出插入信息");
                        System.out.println("输出插入信息");
 name = "insert.csv";
                        name = "insert.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 2)
                }else if(flag == 2) {
{
 System.out.println("输出修改信息");
                        System.out.println("输出修改信息");
 name = "update.csv";
                        name = "update.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else if(flag == 3)
                }else if(flag == 3) {
{
 System.out.println("输出删除信息");
                        System.out.println("输出删除信息");
 name = "delete.csv";
                        name = "delete.csv";
 WriterOut(name,col);
                        WriterOut(name,col);

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        

 public void WriterOut(String name,Collection list) throws IOException
        public void WriterOut(String name,Collection list) throws IOException {
{
 String path = "d:/"+name;
                String path = "d:/"+name;
 
        
 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
 int count = list.size();
        int count = list.size();

 String csvData[] = new String[count];
        String csvData[] = new String[count];
 
        
 Iterator it = list.iterator();
        Iterator it = list.iterator();
 
        
 int i = 0;
        int i = 0;

 while(it.hasNext())
        while(it.hasNext()) {
{
 String temp = (String)it.next();
                String temp = (String)it.next();
 System.out.println(temp);
                System.out.println(temp);
 csvData[i] = temp;
                csvData[i] = temp;
 i++;
                i++;
 }
        }
 
        
 for(int j = 0;j < csvData.length;j++)
        for(int j = 0;j < csvData.length;j++)

 
                 {
{
 writer.write(csvData[j] + LINE_SEPARATOR);
                        writer.write(csvData[j] + LINE_SEPARATOR);
 
                        
 }
                }
 writer.close();
        writer.close();
 
       
 }
        }
 
        

 public void SelectDB()
        public void SelectDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 Statement stmt  = this.conn.createStatement();
                        Statement stmt  = this.conn.createStatement();
 String SQL = "select id ,name ,age ,address  from test";
                        String SQL = "select id ,name ,age ,address  from test";
 ResultSet rs = stmt.executeQuery(SQL);
                        ResultSet rs = stmt.executeQuery(SQL);

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

 String temp = "";
                        String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");


 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 
         
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }        
 }
                                }
 col.add(temp);   //
                                col.add(temp);   //
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(0,col);
                        getResult(0,col);
 closeConnection();
                        closeConnection();

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

 public void InsertDB()
        public void InsertDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "insert into test values('0011','sandy','25','shanghai')";
                        String SQL = "insert into test values('0011','sandy','25','shanghai')";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();

 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
            

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(1,col);
                        getResult(1,col);
 closeConnection();
                        closeConnection();

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

 public void UpdateDB()
        public void UpdateDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "update test set address = '上海'";
                        String SQL = "update test set address = '上海'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);

 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(2,col);
                        getResult(2,col);
 closeConnection();
                        closeConnection();

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

 public void DeleteDB()
        public void DeleteDB() {
{

 try
                try {
{
 getConnection();
                        getConnection();
 String SQL = "delete from test where id = '0008'";
                        String SQL = "delete from test where id = '0008'";
 PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
                        PreparedStatement pstmt  = this.conn.prepareStatement(SQL);
 
                        
 ResultSet rs = pstmt.executeQuery();
                        ResultSet rs = pstmt.executeQuery();        
 conn.commit();
                        conn.commit();
 
                        
 SQL = "select id ,name ,age ,address  from test";
                        SQL = "select id ,name ,age ,address  from test";
 Statement stmt = this.conn.createStatement();
                        Statement stmt = this.conn.createStatement();
 rs = stmt.executeQuery(SQL);
                        rs = stmt.executeQuery(SQL);
 ResultSetMetaData ss = rs.getMetaData();
                        ResultSetMetaData ss = rs.getMetaData();        
 int count = ss.getColumnCount();
                        int count = ss.getColumnCount();
 
                        
 Collection col = new ArrayList();
                        Collection col = new ArrayList();
 
                        
 String temp = "";
            String temp = "";
 
                        

 for(int j = 1; j <= count; j++)
                        for(int j = 1; j <= count; j++) {
{
 System.out.print(ss.getColumnName(j)+"        ");
                                System.out.print(ss.getColumnName(j)+"        ");

 if(j == count)
                                if(j == count) {
{
 temp = temp + ss.getColumnName(j);
                                        temp = temp + ss.getColumnName(j);

 }else
                                }else {
{
 temp = temp + ss.getColumnName(j)+",";
                                        temp = temp + ss.getColumnName(j)+",";
 }
                                }
 }
                        }
 col.add(temp);
                        col.add(temp);
 System.out.println(" ");
                        System.out.println(" ");
 
                        

 while(rs.next())
                        while(rs.next()) {
{        
 temp = "";
                                temp = "";

 for (int i = 1; i <= count;i++)
                                for (int i = 1; i <= count;i++) {
{
 System.out.print(rs.getString(i)+" ");
                                        System.out.print(rs.getString(i)+" ");

 if(i == count)
                                        if(i == count) {
{
 temp = temp + "\""+ rs.getString(i) + "\"";
                                                temp = temp + "\""+ rs.getString(i) + "\"";

 }else
                                        }else {
{
 temp = temp + "\""+ rs.getString(i) + "\",";
                                                temp = temp + "\""+ rs.getString(i) + "\",";
 }
                                        }
 }
                                }
 col.add(temp);
                                col.add(temp);
 System.out.println(" ");
                                System.out.println(" ");
 }
                        }
 getResult(3,col);
                        getResult(3,col);
 closeConnection();
                        closeConnection();

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

 public void excute(int tag)
        public void excute(int tag) {
{

 if(tag == 0)
                if(tag == 0) {
{
 System.out.println("进行查询操作");
                        System.out.println("进行查询操作");
 SelectDB();
                        SelectDB();

 }else if(tag == 1)
                }else if(tag == 1) {
{
 System.out.println("进行添加操作");
                        System.out.println("进行添加操作");
 InsertDB();
                        InsertDB();

 }else if(tag == 2)
                }else if(tag == 2) {
{
 System.out.println("进行修改操作");
                        System.out.println("进行修改操作");
 UpdateDB();
                        UpdateDB();

 }else if(tag == 3)
                }else if(tag == 3) {
{
 System.out.println("进行删除操作");
                        System.out.println("进行删除操作");
 DeleteDB();
                        DeleteDB();

 }else
                }else {
{
 System.out.println("标志码输入错误");
                        System.out.println("标志码输入错误");
 }
                }
 }
        }
 
        
 public static void main(String[] args)
        public static void main(String[] args) 

 
         {
{
 ContactTest  ct = new ContactTest ();
                ContactTest  ct = new ContactTest ();
 ct.flag = 0;  //0-查询;1-添加;2-修改;3-删除
                ct.flag = 0;  //0-查询;1-添加;2-修改;3-删除
 ct.excute(ct.flag);
                ct.excute(ct.flag);
 }
        }
 
        
 }
}
