求一个好的连接数据库的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;
public Connection con = null;
public String sql = null;
Statement stmt = null;
public ResultSet result = null;
private int affectedRows = 0;
public DBConnBean()
{
xdebug = true;
con = null;
sql = null;
}
public Connection Connect()
throws Exception
{
String msg = null;
try
{
Class.forName(DBDriver).newInstance();
}
catch(Exception e)
{
msg = "加载数据库驱动失败 ";
if (xdebug) msg += "(驱动´ "+DBDriver+ "´) ";
throw new Exception(msg);
}
try
{
String conStr = conp;
con = DriverManager.getConnection(conStr,username,password);
}
catch(SQLException e)
{
msg = "!!数据库连接失败 ";
if (xdebug)
{
msg += "(错误信息=´ " + e.getMessage()+ "´ SQL状态值=´ " + e.getSQLState()+ "´ 错误代码=´ " + e.getErrorCode()+ "´) ";
}
throw new Exception(msg);
}
return con;
}
protected void finalize()
throws Throwable
{
super.finalize();
if (stmt != null) stmt.close();
if (result != null) result.close();
}
//最近一次对数据库查询受影响的行数
public int getAffectedRows()
{
return affectedRows;
}
public Connection getCon()
{
return con;
}
public String getConp()
{
return conp;
}
public String getDBDriver()
{
return DBDriver;
}
public String getDBName()
{
return DBName;
}
public boolean getDebug()
{
return xdebug;
}
public String getPassword()
{
return password;
}
public ResultSet getResult()
{
return result;
}
public String getSql()
{
return sql;
}
public String getUsername()
{
return username;
}
public void over()
throws Throwable
{
finalize();
}
public ResultSet query()
throws Exception
{
result = null;
affectedRows = 0;
if (con == null)
Connect();
if (stmt == null)
stmt = con.createStatement();
if (sql.substring(0,6).equalsIgnoreCase( "select "))
{
result = stmt.executeQuery(sql);
}
else
{
affectedRows = stmt.executeUpdate(sql);
}
return result;
}
public ResultSet query(String s)
throws Exception
{
sql = s;
return query();
}
public void setDBDriver(String s)
{
DBDriver = s;
}
public void setDebug(boolean b)
{
xdebug = b;
}
public void setgetConp(String s)
{
conp = s;
}
public void setgetDBName(String s)
{
DBName = s;
}
public void setgetUsername(String s)
{
username = s;
}
public void setPassword(String s)
{
password = s;
}
public void setSql(String s)
{
sql = s;
}
}
② DBQueryBean.Java的源代码如下所示:
package dbaccess;
import Java.sql.*;
import Java.util.*;
import Java.io.*;
import Java.lang.reflect.*;
public class DBQueryBean
implements Serializable
{
DBConnBean dbc;
String sql = null;
int rowcount = 0;
int colcount = 0;
// int limitcount = 0;
Vector result = null;
public String _WATCH = " ";
public DBQueryBean()
{
dbc = new DBConnBean();
try {
dbc.Connect();
} catch(Exception e) {
handleException(e);
}
}
protected void finalize()
throws Throwable
{
super.finalize();
if (dbc != null) dbc.over();
if (result != null) result.removeAllElements();
}
public String get(int row, int col)
{
if (result==null || row >= result.size()) return null;
String r[] = (String[])result.elementAt(row);
if (col >= Java.lang.reflect.Array.getLength(r)) return null;
return r[col];
}
public int getAffRows() { return dbc.getAffectedRows(); }
public int getColumncount() {
return colcount;
}
public String[] getRow(int row)
{
if (result==null || row >= result.size()) return null;
return (String [])result.elementAt(row);
/*String ret[] = new String[colcount];
Vector r = (Vector)result.elementAt(row);
for (int i=0; i<colcount; i++)
ret[i] = (String)r.elementAt(i);
return ret;*/
}
public int getRowcount() {
return rowcount;
}
public void handleException(Exception e)
{
_WATCH = e.getMessage();
}
public void init()
{
rowcount = 0;
colcount = 0;
// limitcount = 0;
result = null;
}
public void over()
throws Throwable
{
finalize();
}
public int query(String sql)
{
result = new Vector();
int ret = 0;
try {
ResultSet rs = dbc.query(sql);
if (rs == null)
{
ret = dbc.getAffectedRows();
}
else
{
ResultSetMetaData rm = rs.getMetaData();
colcount = rm.getColumnCount();
while (rs.next())
{
String row[] = new String[colcount];
for (int i=0; i<colcount; i++)
row[i] = rs.getString(i+1);
result.addElement(row);
rowcount++;
}
rs.close(); // to release the resource.
ret = result.size();
}
}
catch(Exception e)
{
handleException(e);
return -1;
}
return ret;
}
}
这个好么?
[解决办法]
连接数据库的类还是越简单越好
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.sql.*;
import java.util.*;
import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
public class Tdb
{
private Connection con=null;//连接
private Statement st=null;//执行SQL语句的对象
private ResultSet rs=null;//记录集对象
public Connection getConnection()
{
try {
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context)initCtx.lookup( "java:comp/env ");
DataSource ds = (DataSource)envCtx.lookup( "tel ");
con=ds.getConnection();
//测试连接
if(con!=null)
{
System.out.println( "ok ");
}
return con;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public ResultSet executeQuery(String sql){
try{
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(sql);
System.out.println( "11 ");
}
catch(SQLException ex){
ex.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql){
int result = 0;
try{
st = con.createStatement();
result = st.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
return result;
}
public void close(){
if (con!=null){
try{
con.close();
con=null;
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
}
}
}
[解决办法]
不一定适合你,但是正确.