Posted on 2006-04-23 15:32
叶舟 阅读(213)
评论(0) 编辑 收藏 所属分类:
Java技术文档
在目录下增加config.properties文件,内容为:
db_driver = com.pointbase.jdbc.jdbcUniversalDriver
db_url = jdbc:pointbase:server://localhost:9092/test
db_user = PBPUBLIC
db_password = PBPUBLIC
格式一定要按照 key = value 的写法
-----------------------------------------------------------------------------------------------------------
主程序:
package ConnectionTest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
import java.util.Properties;
public class ContactTest {
private Connection conn = null;
private int flag;
static final String LINE_SEPARATOR = System.getProperty("line.separator");
public void getConnection(){
try{
/*
* 修改了程序,通过从配置文件中取得数据库信息
*/
File file = new File("./src/test/ConnectionTest/config.properties"); //确定你的目录路径
Properties props = new Properties();
props.load(new FileInputStream(file));
String JDBC_URL = props.getProperty("db_url");
String JDBC_DRIVER= props.getProperty("db_driver");;
String USER = props.getProperty("db_user");;
String PASSWORD = props.getProperty("db_password");;
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 = 0; //0-查询;1-添加;2-修改;3-删除
ct.excute(ct.flag);
}
}