当WebLogic中设置数据库连接池后,在JSP或Java Bean中需要操作数据库时,是不是就需要有建立数据库连接的代码了?
当WebLogic中设置数据库连接池后,在JSP或Java Bean中需要操作数据库时,是不是就需要有建立数据库连接的代码了?
(如:
Class.forName("...");
Connetion conn=DriverManager.getConnectio("...");
Statement stmt=conn.createStatement(); )
是不是就可以直接执行executeQuery()和executeUpdate()语句了?如果不是的话,具体应该怎么样的?
这是我改过的原来用jdbc连接的代码,被注视的是直接连接的,现有的是pool的。
package UsefulClasses;
import java.sql.*;
import java.util.*;
import javax.naming.*;
public class Database
{
//String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//String sConnStr = "jdbc:odbc:Mechanism";
//String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Mechanism";
Context ctx = null;
Hashtable ht=null;
javax.sql.DataSource ds = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public Database()
{
ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://127.0.0.1:80");
try
{
ctx = new InitialContext(ht);
ds = (javax.sql.DataSource)ctx.lookup("Mechanism");
//Class.forName(sDBDriver);
}
catch(javax.naming.NamingException e)
{
System.err.println("SQL Driver not found:" + e.getMessage());
}
}
public void executeInsert(String sql)
{
try
{
//conn = DriverManager.getConnection(sConnStr,"","");
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQL insert error:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
//conn = DriverManager.getConnection(sConnStr,"user","111111");
conn = ds.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("SQL query error:"+ex.getMessage());
}
return rs;
}
public ResultSet executePreparedQuery(String sql)
{
try
{
//conn = DriverManager.getConnection(sConnStr,"","");
//stmt = conn.createPreparedStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//rs = stmt.executePreparedQuery(sql);
conn = ds.getConnection();
stmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(SQLException ex)
{
System.err.println("SQL prepared query error:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
try
{
//conn = DriverManager.getConnection(sConnStr,"user","111111");
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQL update error: " + ex.getMessage());
}
}
public void executeDelete(String sql)
{
try
{
//conn = DriverManager.getConnection(sConnStr,"","");
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQL delete error:"+ex.getMessage());
}
}
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}