求一个好的连接数据库的Bean,如果好一共赠送200分。
求一个好的连接数据库的Bean,如果好一共赠送200分。
------解决方案--------------------什么叫好 能用不就行么
------解决方案--------------------软件以用为本!
------解决方案--------------------楼主何谓好、不好?我认为可以使用连接池来简化链接,或者把链接的代码封到一个类似ConnectionFactory的类里啊。
===========================================================
package pkg;
import java.sql.*;
import javax.sql.*;
import javax.naming.InitialContext;
import
javax.naming.NamingException;
public class ConnectionFactory {
private static DataSource dataSource = null;
private static ConnectionFactory ref = new ConnectionFactory();
private String jndiName = "java:comp/env/jdbc/webQuery ";
private ConnectionFactory() {
loadDBPoolDriver();
//loadDBNormalDriver();
}
/**
* 使用DataSource
*
*/
private void loadDBPoolDriver(){
try{
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup(jndiName);
}catch(NamingException ne){
ne.printStackTrace();
}
}
/**
* 一般注册
*
*/
private void loadDBNormalDriver(){
try{
Class.forName(DatabaseNames.DBDRIVER);
}catch(
ClassNotFoundException cnfe){
cnfe.printStackTrace();
cnfe.printStackTrace();
}
}
/**
* 连接方式选择(Pool,Normal)
*
* @param isPoolConnect boolean
* @throws
SQLException * @return Connection
*/
public static Connection getConnection(boolean isPoolConnect) throws SQLException{
if(isPoolConnect){
return dataSource.getConnection();
}
else{
return DriverManager.getConnection(DatabaseNames.DBURL,
DatabaseNames.DBUSER,
DatabaseNames.DBPWD);
}
}
//................................
}
------解决方案--------------------两个类
package com.webrm.database;
public interface DBResource {
String url= "jdbc:oracle:thin:@localhost:1521:app97 ";
String username= "develop ";
String password= "oracle817 ";
String driver= "oracle.jdbc.driver.OracleDriver ";
String myurl= "jdbc:mysql://127.0.0.1:3306/netoa ";
String myusername= "root ";
String mypassword= " ";
String mydriver= "org.gjt.mm.mysql.Driver ";
}
------解决方案--------------------很明显,楼上的冲着200分来的,哈哈。
------解决方案--------------------package dbaccess;
import Java.sql.*;
import Java.util.*;
import Java.io.*;
public class DBConnBean
implements Serializable{
private String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver ";
private String DBHost = "127.0.0.1 ";
private String DBName = "demo ";
private String conp = "jdbc:odbc:db_demo ";
private String username = " ";
private String password = " ";
private boolean xdebug = true;
publi