以下问题是这一期间回答waterlily 关于java的一些整理,发现对大家都很有作用,现在将这些详细讲解的程序单独列出来供大家参考,学习,讨论
------------------------------------------------------------
1.使用jdbc连接数据库--通过jdbc连接数据库,将SQL语句运行结果打印到控制台
package ConnectionTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class ContactTest {
private Connection conn = null;
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 excute(){
try{
Statement stmt = this.conn.createStatement();
String SQL = "select id ,name ,age ,address from test";
ResultSet rs = stmt.executeQuery(SQL);
ResultSetMetaData ss = rs.getMetaData();
int count = ss.getColumnCount();
for(int j = 1; j <= count; j++){
System.out.print(ss.getColumnName(j)+" ");
}
System.out.println(" ");
while(rs.next()){
for (int i = 1; i <= count;i++){
System.out.print(rs.getString(i)+" ");
}
System.out.println(" ");
}
rs.close();
stmt.close();
this.conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args)
{
ContactTest ct = new ContactTest ();
ct.getConnection();
ct.excute();
}
}
------------------------------------------------------------
2.在上题基础上增加对文件的操作--将从数据库得到的信息选择性的放到不同的txt文件中
package ConnectionTest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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;
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.txt";
WriterOut(name,col);
}else if(flag == 1){
System.out.println("输出插入信息");
name = "insert.txt";
WriterOut(name,col);
}else if(flag == 2){
System.out.println("输出修改信息");
name = "update.txt";
WriterOut(name,col);
}else if(flag == 3){
System.out.println("输出删除信息");
name = "delete.txt";
WriterOut(name,col);
}else{
System.out.println("标志码输入错误");
}
}
public void WriterOut(String name,Collection list) throws IOException{
String path = "d:/"+name;
File newFile = new File(path); //这里注意你的输入文件的路径
BufferedWriter wirter = new BufferedWriter(new FileWriter(newFile));
Iterator it = list.iterator();
while(it.hasNext()){
String temp = (String)it.next();
System.out.println(temp);
wirter.newLine();
wirter.write(temp);
wirter.flush();
}
wirter.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)+" ");
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)+" ");
temp = temp + rs.getString(i)+" ";
}
col.add(temp); //
System.out.println(" ");
}
getResult(0,col);
rs.close();
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();
this.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)+" ");
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)+" ");
temp = temp + rs.getString(i)+" ";
}
col.add(temp);
System.out.println(" ");
}
getResult(1,col);
rs.close();
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();
this.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)+" ");
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)+" ");
temp = temp + rs.getString(i)+" ";
}
col.add(temp);
System.out.println(" ");
}
getResult(2,col);
rs.close();
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();
this.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)+" ");
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)+" ");
temp = temp + rs.getString(i)+" ";
}
col.add(temp);
System.out.println(" ");
}
getResult(3,col);
rs.close();
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);
}
}
------------------------------------------------------------
3.将上例中的txt文档全部替换成csv文档(表格文档)
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);
}
}
-----------------------------------------------------------------------------------------------------------
4.在上例的基础上对程序进行了修改,通过从配置文件中读取数据库信息
在目录下增加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);
}
}
-----------------------------------------------------------------------------------------------------------
5.在上例的基础上修改程序,通过xml配置文件得到数据库信息
更新程序:
-----------------------------------------------------------------------------------------------------------
当前目录下新建config.xml文件:
文件内容:
<Config>
<DBConfig>
<driver>com.pointbase.jdbc.jdbcUniversalDriver</driver> //driver信息
<url>jdbc:pointbase:server://localhost:9092/test</url> //url信息
<user>PBPUBLIC</user> //数据库user
<password>PBPUBLIC</password> //数据库password
</DBConfig>
<DBOperation>
<Select>
<SQL>select id ,name ,age ,address from test</SQL> //sql语句,用来查询
<Text>select.csv</Text> //查询结果的输出文件名
</Select>
<Insert>
<SQL>insert into test values('0011','sandy','25','shanghai')</SQL>
<Text>insert.csv</Text>
</Insert>
<Update>
<SQL>update test set address = '上海'</SQL>
<Text>update.csv</Text>
</Update>
<Delete>
<SQL>delete from test where id = '0008'</SQL>
<Text>delete.csv</Text>
</Delete>
</DBOperation>
</Config>
--------------------------------------------------------------------------------------------------------
新建一个.java文件,专门用来解析xml,这里我用的是开源的工具jdom,现在比较流行
新建文件:XmlParse.java 在同一个package内
--------------------------------------------------------------------------------------------------------
package ConnectionTest;
import java.io.IOException;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class XmlParse {
private HashMap configTable= new HashMap();
public HashMap parseXML() throws JDOMException, IOException{
SAXBuilder builder = new SAXBuilder();
Document readDoc = builder.build("src.test/ConnectionTest/config.xml");
Element config = readDoc.getRootElement();
System.out.println(config.getName());
Element e = config.getChild("DBConfig");
HashMap dbConfig = new HashMap();
configTable.put("dbconfig_driver",e.getChildText("driver"));
configTable.put("dbconfig_url",e.getChildText("driver"));
configTable.put("dbconfig_user",e.getChildText("user"));
configTable.put("dbconfig_password",e.getChildText("password"));
HashMap dbOper = new HashMap();
e = config.getChild("DBOperation");
Element c = e.getChild("Select");
configTable.put("dboperation_selectSQL",c.getChildText("SQL"));
configTable.put("dboperation_selectText",c.getChildText("Text"));
c = e.getChild("Insert");
configTable.put("dboperation_insertSQL",c.getChildText("SQL"));
configTable.put("dboperation_insertText",c.getChildText("Text"));
c = e.getChild("Update");
configTable.put("dboperation_updateSQL",c.getChildText("SQL"));
configTable.put("dboperation_updateText",c.getChildText("Text"));
c = e.getChild("Delete");
configTable.put("dboperation_deleteSQL",c.getChildText("SQL"));
configTable.put("dboperation_deleteText",c.getChildText("Text"));
return configTable;
}
}
--------------------------------------------------------------------------------------------------------
最后是主程序修改:
--------------------------------------------------------------------------------------------------------
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.HashMap;
import java.util.Iterator;
import org.jdom.JDOMException;
public class ContactTest {
private Connection conn = null;
private int flag;
private HashMap config = new HashMap(); //存放从config.xml中得到的配置信息
static final String LINE_SEPARATOR = System.getProperty("line.separator");
/*
* 增加了构造器,得到解析后的xml的配置信息列表
*/
public ContactTest() throws JDOMException, IOException{
XmlParse xml = new XmlParse();
config = xml.parseXML();
}
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");
*/
String JDBC_URL = (String)config.get("dbconfig_url");
String JDBC_DRIVER= (String)config.get("dbconfig_driver");
String USER = (String)config.get("dbconfig_user");
String PASSWORD = (String)config.get("dbconfig_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 = (String)config.get("dboperation_selectText"); //从配置中得到文件名,不再是固定文件名了
WriterOut(name,col);
}else if(flag == 1){
System.out.println("输出插入信息");
name = (String)config.get("dboperation_insertText");;
WriterOut(name,col);
}else if(flag == 2){
System.out.println("输出修改信息");
name = (String)config.get("dboperation_updateText");;
WriterOut(name,col);
}else if(flag == 3){
System.out.println("输出删除信息");
name = (String)config.get("dboperation_deleteText");;
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 = (String)config.get("dboperation_selectSQL");
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 = (String)config.get("dboperation_insertSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");;
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 = (String)config.get("dboperation_updateSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");
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 = (String)config.get("dboperation_deleteSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");
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) throws JDOMException, IOException
{
ContactTest ct = new ContactTest ();
ct.flag = 0; //0-查询;1-添加;2-修改;3-删除
ct.excute(ct.flag);
}
}
为了便于你更好理解这些程序,我增加了注释,建议使用editplus来看,这样比较好区分注释
config.xml:
----------------------------------------------------------------------------------------------------------
<!----------首先是根节点<config>--------->
<Config>
<!----------然后是子节点<DBConfig>,用来存放数据库的配置信息--------->
<DBConfig>
<!-------在该节点下的第一个配置参数<dirver>:数据库驱动,<driver></driver>是标签,当中就是你的配置信息------->
<driver>com.pointbase.jdbc.jdbcUniversalDriver</driver>
<!-------在该节点下的第二个配置参数<url>:数据库url地址--------->
<url>jdbc:pointbase:server://localhost:9092/test</url>
<!-------在该节点下的第三个配置参数<user>:数据库用户名--------->
<user>PBPUBLIC</user>
<!-------在该节点下的第四个配置参数<password>:数据库密码--------->
<password>PBPUBLIC</password>
</DBConfig>
<!----------然后是子节点<DBOperation>,用来存放对数据库的操作信息,这个和<DBConfig>是同一个级别的--------->
<DBOperation>
<!-------在该节点下的第一个子节点<select>:对数据库进行select操作--------->
<Select>
<!-------在该子节点下有两个具体的配置参数--------->
<!-------配置参数<SQL>,对数据库进行select的操作的查询语句,就是将在程序中的查询语句在这里定义了------>
<SQL>select id ,name ,age ,address from test</SQL>
<!--配置参数<Text>,根据你的业务需求需要在不同的数据库操作后输出不同的文件信息,这里存放输出的文件名-->
<Text>select.csv</Text>
</Select>
<!-------在该节点下的第二个子节点<insert>:对数据库进行insert操作--------->
<Insert>
<SQL>insert into test values('0011','sandy','25','shanghai')</SQL>
<Text>insert.csv</Text>
</Insert>
<!-------在该节点下的第三个子节点<update>:对数据库进行update操作--------->
<Update>
<SQL>update test set address = '上海'</SQL>
<Text>update.csv</Text>
</Update>
<!-------在该节点下的第四个子节点<delete>:对数据库进行delete操作--------->
<Delete>
<SQL>delete from test where id = '0008'</SQL>
<Text>delete.csv</Text>
</Delete>
</DBOperation>
</Config>
XmlParse.java 增加注释:
-----------------------------------------------------------------------------------------------------------
package ConnectionTest;
import java.io.IOException;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/*
* 对xml进行解析,从xml中拿到所有的配置信息,放在集合中,采用HashMap集合方式
* HashMap是采用key = value的方式存放的,所以可以通过对应的key值得到相应的配置信息
* 这个和xml的标签和标签内容是一一对应的,例如<user>public</user>是标签,那么在
* HashMap中这配置信息的就可以存放为,key = "user",value = "public"来存放
* 具体的参照API文档
*/
public class XmlParse {
private HashMap configTable= new HashMap(); //生成一个HashMap,用来存放所有的配置信息
/*
* 方法parseXML,具体执行解析xml文件
* 返回一个HashMap,就是将解析完成的含有配置信息的HashMap返回
*/
public HashMap parseXML() throws JDOMException, IOException{
//固定格式,先生成解析器,再建立Document
SAXBuilder builder = new SAXBuilder();
//将你要解析的xml文件导入,注意文件的路径,总是以工程文件的根目录为起点的
Document readDoc = builder.build("src.test/ConnectionTest/config.xml");
//Element为xml中元素节点,这里通过getRootElement()方法得到根节点
Element config = readDoc.getRootElement();
//通过根节点得到一个名字为"DBConfig"的子节点
Element e = config.getChild("DBConfig");
//在这个子节点下有四个具体的配置信息,我们通过e.getChildText()这个
//方法得到里面的具体的配置信息,就是<></>标签中间的部分,"driver"就是
//标签的名字
//再通过HashMap的put(key,value)的方法将这些信息保存到HashMap中
configTable.put("dbconfig_driver",e.getChildText("driver"));
configTable.put("dbconfig_url",e.getChildText("url"));
configTable.put("dbconfig_user",e.getChildText("user"));
configTable.put("dbconfig_password",e.getChildText("password"));
//再次通过根节点得到一个名字为"DBOperation"的子节点
e = config.getChild("DBOperation");
//找到该子节点下的一个名字为"Select"的子节点
Element c = e.getChild("Select");
//同样进行读取节点信息和HashMap的保存操作
configTable.put("dboperation_selectSQL",c.getChildText("SQL"));
configTable.put("dboperation_selectText",c.getChildText("Text"));
//以下同上
c = e.getChild("Insert");
configTable.put("dboperation_insertSQL",c.getChildText("SQL"));
configTable.put("dboperation_insertText",c.getChildText("Text"));
c = e.getChild("Update");
configTable.put("dboperation_updateSQL",c.getChildText("SQL"));
configTable.put("dboperation_updateText",c.getChildText("Text"));
c = e.getChild("Delete");
configTable.put("dboperation_deleteSQL",c.getChildText("SQL"));
configTable.put("dboperation_deleteText",c.getChildText("Text"));
//最后将解析完成,保存后的HashMap返回
return configTable;
}
}
ContactTest.java 增加注释:
--------------------------------------------------------------------------------------------------------
package ConnectionTest;
import java.io.BufferedWriter;
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.HashMap;
import java.util.Iterator;
import org.jdom.JDOMException;
public class ContactTest {
private Connection conn = null; //数据库连接connection
private int flag; //标志位,判断数据库操作
private HashMap config = new HashMap(); //存放从config.xml中得到的配置信息
//常量,分行标志符,从系统中获得的
static final String LINE_SEPARATOR = System.getProperty("line.separator");
/*
* 增加了构造器,得到解析后的xml的配置信息列表
*/
public ContactTest() throws JDOMException, IOException{
XmlParse xml = new XmlParse(); //声明一个对象,解析xml配置文件
config = xml.parseXML(); //调用解析方法,将返回的HashMap结果放入config中
}
/*
* 数据库连接操作
*/
public void getConnection(){
try{
/*
* 修改了程序,通过从配置文件中取得数据库信息
* 这里是从hashmap中拿到配置信息get("dbconfig_url")这个方法
* 是从hashmap中拿出key值为dbconfig_url的value的值,因为放入
* hashmap中的key,value类型都是object的,所以拿出来的时候要转换
* 格式成String
*/
String JDBC_URL = (String)config.get("dbconfig_url");
String JDBC_DRIVER= (String)config.get("dbconfig_driver");
String USER = (String)config.get("dbconfig_user");
String PASSWORD = (String)config.get("dbconfig_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("输出查询信息");
//这里要定义输出文件的文件名字,我们现在也是从hashmap中拿到文件名的值
//就是你在config.xml中看到的<text></text>标签中的文件名
name = (String)config.get("dboperation_selectText");
WriterOut(name,col);
}else if(flag == 1){
System.out.println("输出插入信息");
name = (String)config.get("dboperation_insertText");;
WriterOut(name,col);
}else if(flag == 2){
System.out.println("输出修改信息");
name = (String)config.get("dboperation_updateText");;
WriterOut(name,col);
}else if(flag == 3){
System.out.println("输出删除信息");
name = (String)config.get("dboperation_deleteText");;
WriterOut(name,col);
}else{
System.out.println("标志码输入错误");
}
}
/*
* 对查询的内容进行输出,并且控制输出格式
*/
public void WriterOut(String name,Collection list) throws IOException{
String path = "d:/"+name; //输出路径
//这里定义了一个IO输出的包装器
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
int count = list.size(); //计算总共要输出几行
/*
* 修改程序,改变输出格式
*/
String csvData[] = new String[count]; //这个你要求的csv文件的数据输出的数组
Iterator it = list.iterator(); //定义一个迭代器,用来遍历集合
int i = 0;
//开始遍历集合,不断的从集合中取出纪录,直到遍历结束
while(it.hasNext()){
String temp = (String)it.next();
System.out.println(temp);
csvData[i] = temp; //将取出的纪录放入数组中
i++;
}
//将数组中的纪录通过IO包装器放到文件中
for(int j = 0;j < csvData.length;j++)
{
//需要在输出文件是修改文件格式,达到csv的文本要求
writer.write(csvData[j] + LINE_SEPARATOR);
}
writer.close();
}
public void SelectDB(){
try{
getConnection();
Statement stmt = this.conn.createStatement();
//这里的SQL语句也放到xml文件中去配置了
String SQL = (String)config.get("dboperation_selectSQL");
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)+" ");
/*
* 新增加的语句,为了达到csv文件的输出的格式
*/
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)+" ");
/*
* 新增加的语句,为了达到csv文件的输出的格式
*/
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 = (String)config.get("dboperation_insertSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");;
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 = (String)config.get("dboperation_updateSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");
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 = (String)config.get("dboperation_deleteSQL");
PreparedStatement pstmt = this.conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
conn.commit();
SQL = (String)config.get("dboperation_selectSQL");
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) throws JDOMException, IOException
{
ContactTest ct = new ContactTest ();
ct.flag = 0; //0-查询;1-添加;2-修改;3-删除
ct.excute(ct.flag);
}
}
------------------------------------------------------------------------------------------------
6.java中所有的数据结构都是对象,通过运算符new为他们分配内存堆,这句话怎么理解?
应该说你这句话不太对,java中的一些基本数据类型,如int ,double ,long,boolean等这些基本类型不是对象,所以严格的来说java不是都是对象的,除了这些个之外其他的都是对象了。
java中要产生对象要具有生命力就要做两件事情,一是声明,这个就好像告诉别人你要生孩子了。二就是要具体分配一个空间给这个对象,让这个对象可以有自己的生命力,这个就是new,而空间就是指的内存空间,java中任何可以操作的对象都是需要得到自己的内存空间才能被使用的
------------------------------------------------------------------------------------------------
7.关于Iterator it = list.iterator();
迭代器是通常是和集合一起用的,是一种java的模式,俗称迭代子,用来遍历整个集合,从集合中取数据
举例:
package Iterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorTest {
public static void main(String[] args){
Collection list = new ArrayList(); //定义一个ArrayList数组集合
list.add("3"); //向这个集合中放入数据
list.add("2");
list.add("1");
list.add("4");
list.add("5");
list.add("6");
Iterator it = list.iterator(); // 第一步
while(it.hasNext()){ //第二步
String temp = (String)it.next(); //取出元素
System.out.println(temp);
}
}
}
--------------------------------------------------------------------------------------------------
8.找出在某一个文件夹下的某一类文件
package File;
import java.io.File;
import java.io.FilenameFilter;
public class FileDemo {
public static void main(String[] args){
-->首先声明一个File对象,指明你的文件目录路径,这里是我计算机中的东西,记住一定要是路径
File dir = new File("E:\\JAVA程序\\JAVA code\\copyfile\\core java");
-->Filter是你自己写的一个实现FilenameFilter接口的类,这个接口用用来过滤文件的名字,传入的参数是要过滤的关键字
Filter filter=new Filter("java");
-->这里将通过Filter过滤得到的文件列表放到一个数组中去
String fileList[] = dir.list(filter);
System.out.println("找到文件数:"+fileList.length);
-->下面就是通过遍历这个数组,从数组中取到文件名
for(int I = 0 ; I < fileList.length; I++){
-->因为在下面要对文件进行判断,所以这里要加上路径和文件名,否则下面的判断会因为找不到文件而判断错误
File tmpFile = new File("E:\\JAVA程序\\JAVA code\\copyfile\\core java\\"+fileList[I]);
-->判断找到的这些文件是目录呢还是文件
if(tmpFile.isFile()){
System.out.println("文件:"+tmpFile);
}else{
System.out.println("目录:"+tmpFile);
}
}
}
}
-->实现过滤器接口的类,这个类主要作用是找出所有文件夹下的以extension结尾的文件
class Filter implements FilenameFilter{
String extension;
Filter(String extension)
{
this.extension=extension;
}
public boolean accept(File directory,String filename){
-->这里的filename.endsWith("."+extension);方法就是找以.extension结尾的文件
return filename.endsWith("."+extension);
}
}
我所修改的程序简单的讲就是,查找所有在E:\\JAVA程序\\JAVA code\\copyfile\\core java这个目录下的以.java结尾的文件
-----------------------------------------------------------------------------------------------------------------
9.关于接口的一些问题
Collection months=new ArrayList(); (这个ArrayList和Collection 我都不太明白)
--------------------------------------------------------------------------------------------------------
Collection是集合的一个接口,ArrayList是集合的一种,这里ArrayList实现Collection这个接口,声明一个接口的对象需要new一个实现他的类。
Object s[]=months.toArray(); (这个地方也不明白)
--------------------------------------------------------------------------------------------------------
Object是java中所有类的父类,months是前面生成的集合对象,是一个ArrayList的对象,这个对象有一个方法是toArray(),能够把这个集合中的转换成一个Object类型的对象数组
System.out.println(s[i].toString());(s[i].toString() 打印这个为什么就是数组里面的内容)
---------------------------------------------------------------------------------------------------------
System.out.println()打印出来的是字符串,()内放的应该是String类型的值,这里用了
s[i].toString()这个语句,意思就是把数组中的一个对象转换成String类型然后输出
List months=new ArrayList();(这个地方List要换成ArrayLis才不报错
我也不知道为什么 我想只要上面那个明白了 下面这个也应该就明白了吧)
----------------------------------------------------------------------------------------------------------
这里和Collection是相同的,List是一个集合的接口,ArrayList()是实现这个接口的类,同样也是生成一个接口对象需要new一个实现他的类
==========================================================================================================
还有很多的问题没有列举出来,大家可以看看《查询的问题对各位大侠绝对是容易的,拜托了》这个帖子