📄 db.java
字号:
package com.wxpn.tutorial.util;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/**
* 描述: 数据库连接类
* Copyright (c) 2005-2008 Wang Xining
* @author 王夕宁
* @version 1.0
*/
public class DB {
static String driver = null;
static String server = null;
static String dbuser = null;
static String dbpassword = null;
static int minconn = 0;
static int maxconn = 0;
static double maxconntime = 0.1D;
static String log_properties = null;
static ConnectionPool p = null;
public static String FILE_SEPARATOR = System.getProperty("file.separator");
public static String PHOENIX_HOME = "e:\\wangxining\\projects\\jsptutorial\\eclipse\\workspace\\HelloWorldProject\\HelloWorldProject\\JavaSource\\";
public static void getAtrributes() {
// 通过获得系统属性构造属性类 prop
// Properties prop = new Properties(System.getProperties());
// 在标准输出中输出系统属性的内容
// prop.list(System.out);
Document doc = null;
try {
File docFile = new File(PHOENIX_HOME + FILE_SEPARATOR
+ "systemdb.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
Element root = doc.getDocumentElement();
NodeList dbChilds = root.getChildNodes();
for (int i = 0; i < dbChilds.getLength(); i++) {
if (dbChilds.item(i).getNodeName().equals("driver")) {
driver = dbChilds.item(i).getFirstChild().getNodeValue();
} else if (dbChilds.item(i).getNodeName().equals("server")) {
server = dbChilds.item(i).getFirstChild().getNodeValue();
} else if (dbChilds.item(i).getNodeName().equals("dbuser")) {
dbuser = dbChilds.item(i).getFirstChild().getNodeValue();
} else if (dbChilds.item(i).getNodeName().equals("dbpassword")) {
dbpassword = dbChilds.item(i).getFirstChild() == null ? ""
: dbChilds.item(i).getFirstChild().getNodeValue();
} else if (dbChilds.item(i).getNodeName().equals("minconn")) {
minconn = dbChilds.item(i).getFirstChild() == null ? 5
: Integer.parseInt(dbChilds.item(i).getFirstChild()
.getNodeValue());
} else if (dbChilds.item(i).getNodeName().equals("maxconn")) {
maxconn = dbChilds.item(i).getFirstChild() == null ? 20
: Integer.parseInt(dbChilds.item(i).getFirstChild()
.getNodeValue());
} else if (dbChilds.item(i).getNodeName().equals("maxconntime")) {
maxconntime = dbChilds.item(i).getFirstChild() == null ? 0.1D
: Double.parseDouble(dbChilds.item(i)
.getFirstChild().getNodeValue());
}
} // end for
} catch (Exception e) {
System.out.println("Problem parsing the file:" + e);
}
}
/**
* Create a connectionpool
*/
public static ConnectionPool getConnPool() {
try {
if (p == null) {
getAtrributes();
p = new ConnectionPool(driver, server, dbuser, dbpassword,
minconn, maxconn, maxconntime);
}
return p;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getLogProperties() {
return PHOENIX_HOME + FILE_SEPARATOR + "conf" + FILE_SEPARATOR
+ "log4j.properties";
}
public static void main(String[] args) {
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
int nextMessageid = 0;
String relative_path = null;
String sql = "select * from user";
stmt = conn.createStatement();
// stmt.addBatch(sql);
// sql = "insert into
// mms_user(messageid,emailid,messagename,sender,coypto,bcc,sendtime,size,totalnumber,priority,remark,content)
// values('7','1','Fw: 中文标识
// 57567567','<wxn1@localhost>','<12121@localhost>','','Sun Oct 03
// 14:30:13 CST 2004','114421','2','3','','')";
sql = new String(sql.getBytes("gb2312"), "8859_1");
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id"));
}
// stmt.executeUpdate(sql);
// stmt.addBatch(sql);
//
// stmt.executeBatch();
// stmt.clearBatch();
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
connPool.freeConnection(conn);
System.out.println("free connection!");
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
}
}
System.out.print("testt");
// System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -