答案:JAVA数据库工具类,读取数据源,配置文件的方法都有,GOOD LUCK!
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.Collections;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBUtil{
public String db_ip;
public String db_port;
public String db_uid;
public String user;
public String password;
public String db_driver;
public String db_url;
public String db_type;
public String Sqlstring;
public Connection db_conn;
public Statement db_stmt;
public ResultSet db_rset;
public int record_count;
public int page_count;
public int rows_perpage;
public int current_page;
public int current_page_rows;
/**
* 通过数据源连接数据库
* @param datasourcename String
*/
public void dbconnbyDatasource(String datasourcename){
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup(datasourcename);
db_conn = ds.getConnection();
db_stmt = db_conn.createStatement();
} catch (Exception e) {
System.out.println("数据库连接错误:"+datasourcename);
}
}
/**
直接获取数据源,
*/
public void getdbconnDatasource(){
dbconnbyDatasource("PortalDataSource");
}
/**
* 通过配置文件连接数据库
* @param datasourcename String
*/
public void dbconnbyFile(String filename){
File file = new File(filename);
String s = file.getAbsolutePath().replace('\\', '/');
Properties properties = new Properties();
try {
FileInputStream fileinputstream = new FileInputStream(s);
properties.load(fileinputstream);
if(fileinputstream != null)
fileinputstream.close();
}
catch(IOException ioexception) {
System.out.println("不能打开配置文件:"+filename);
return;
}
db_type = properties.getProperty("db_type");
user = properties.getProperty("user");
password = properties.getProperty("password");
byte byte0 = 2;
if(db_type.equals("oracle"))
byte0 = 1;
switch(byte0)
{
default:
break;
case 1: // '\001'
db_ip = properties.getProperty("db_ip");
db_port = properties.getProperty("db_port");
db_uid = properties.getProperty("db_uid");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println("Could not load the driver.");
}
String s1 = user + "/" + password + "@" + db_ip + ":" + db_port + ":" + db_uid;
try {
db_conn = DriverManager.getConnection("jdbc:oracle:thin:" + s1);
db_stmt = db_conn.createStatement();
}
catch(SQLException sqlexception) {
System.out.println("Creat connection error.");
}
break;
case 2: // '\002'
db_driver = properties.getProperty("db_driver");
db_url = properties.getProperty("db_dsn");
try {
Class.forName(db_driver);
}
catch(ClassNotFoundException classnotfoundexception1) {
classnotfoundexception1.printStackTrace();
}
try {
db_conn = DriverManager.getConnection(db_url, user, password);
db_stmt = db_conn.createStatement();
}
catch(SQLException sqlexception1) {
System.out.println("Creat connection error.");
}
break;
}
}
/**
* 关闭数据库连接
*/
public void dbClose(){
if (db_rset != null) try{
db_rset.close();
}catch(java.sql.SQLException ex){
ex.printStackTrace();
}
if (db_stmt != null) try{
db_stmt.close();
}catch(java.sql.SQLException ex){
ex.printStackTrace();
}
if (db_conn != null) try{
db_conn.close();
}catch(java.sql.SQLException ex){
ex.printStackTrace();
}
}
/**
* 执行更新、插入、删除、建表等不需要返回结果的SQL语句
*/
public int executeSql(String updatesql) throws SQLException {
int i=0;
Statement db_stmts = db_conn.createStatement();
i= db_stmts.executeUpdate(updatesql);
db_stmts.close();
return i;
}
/**
* 执行select语句,返回结果数据集List
*/
public List queryDatalist(String querystr) throws SQLException {
countRecord(querystr);
if (record_count == 0)
return null;
Statement db_stmts = db_conn.createStatement();
db_rset = db_stmts.executeQuery(querystr);
List list = resultSetToList(db_rset);
db_stmts.close();
return list;
}
/**
* 执行select语句,返回结果数据集ResultSet
*/
public ResultSet queryResultset(String querystr) throws Exception {
countRecord(querystr);
if (record_count == 0)
return null;
db_rset = db_stmt.executeQuery(querystr);
return db_rset;
}
/**
* 将查询结果封装成List。br
* List中元素类型为封装一行数据的Map,Map key为字段名(大写),value为相应字段值
* @param rs ResultSet
* @return List
* @throws java.sql.SQLException
*/
public List resultSetToList(ResultSet rs) throws SQLException{
if (rs==null) return Collections.EMPTY_LIST;
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
List list = new ArrayList();
HashMap rowData;
current_page_rows = 0;
while (rs.next()){
rowData = new HashMap(columnCount);
for (int i=1; i=columnCount; i++){
rowData.put(md.getColumnName(i).toLowerCase(),rs.getObject(i));
}
list.add(rowData);
current_page_rows++;
}
return list;
}
}
其他:1.定义3个连接方式
2.根据数据库的type,以及id区别连接方式
3.封装方式很重要,这里面的参数取决于你的链接方式
上一个:sql数据库.mdf如何导入到access中,在SQL SERVER2005,没有企业管理器的情况下,并且数据实时更新。
下一个:计算机大作业要用ACCESS做个有关NBA的数据库,跪求构思