高分!!!求JSP连接ORACLE 10数据库的bean

guxinglou 2004-09-10 12:01:22
如题,最好给上jsp文件的代码及java代码,谢谢。

分不够可以再加的。

...全文
123 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yujiabian 2004-09-11
  • 打赏
  • 举报
回复
你要的东西发给你了!我发到你的信箱:13787272282@hnmcc.com,我的信箱是:yujiabian_hginfo@163.com
ynniebo 2004-09-11
  • 打赏
  • 举报
回复
我曾也在其他地方也加答过这类的问题.但来了也就给你发一下吧.
但是要加载ojdbc14.jar这个包.也就是直接考到web-inf/lib/..就行了..........
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class Database extends Object implements java.io.Serializable {
private String driver=null;
private String url=null;
private String user=null;
private String pass=null;

private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null; //结果集
public Database() {
}

//连接池
public boolean getConnFromPool(String sourceName){
try{
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup(sourceName);
conn=ds.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
return true;
}catch(Exception e){
if (stmt!=null){
try{
stmt.close();
}catch(Exception e1){}
}
if (conn!=null){
try{
conn.close();
}catch(Exception e1){}
}
System.out.println(e.getMessage());
return false;
}
}

//连接数据库
public void getConnection(){
driver="oracle.jdbc.driver.OracleDriver";
String connStr = "jdbc:oracle:thin:@127.0.0.1:1521:testdb"; //thin con
url="";
user="sys";
pass="12345";

try{
Class.forName(driver);
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("driver error:");}
try{
conn=DriverManager.getConnection(connStr,user,pass);

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception e){
System.out.println("error:"+e.getMessage());
}

}


//关闭连接
public boolean closeConnection(){
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
rs=null;
stmt=null;
conn=null;
return true;
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
if (stmt!=null){
try{
stmt.close();
stmt=null;
}catch(Exception e){}
}
if (conn!=null){
try{
conn.close();
conn=null;
}catch(Exception e){}
}
}
return false;
}

//查询数据库
public ResultSet query(String sql){
try{
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println(e.getMessage());
return null;
}
return rs;
}

// 查询数据库
public void in_query(String sql){
try{
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println(e.getMessage());
}
}

//更新数据库
public int update(String sql){
int i=0;
try{
i=stmt.executeUpdate(sql);
}catch(Exception e){
System.out.println(e.getMessage());
}
return i;
}

//插入数据库
public int insert(String sql){
int i=0;
try{
i=stmt.executeUpdate(sql);
}catch(Exception e){
System.out.println(e.getMessage());
}
return i;
}

//测试
public static void main(String[] args){
Database db=new Database();
try{
ResultSet rs=null;
db.getConnection();
String sql="select * from test";
rs=db.query(sql);
while(rs.next()){
System.out.print(rs.getString(1));
System.out.print(" ");
System.out.print(rs.getString(2));
System.out.print(" ");
System.out.print(rs.getString(3));
System.out.println(" ");
}
db.closeConnection();
}catch(Exception e){}
}
}

应该网 2004-09-10
  • 打赏
  • 举报
回复
代码有点长,发至你邮箱好了!
应该网 2004-09-10
  • 打赏
  • 举报
回复
/**
* Returns an open connection. If no one is available, and the max
* number of connections has not been reached, a new connection is
* created.
*
* @param name The pool name as defined in the properties file
* @return Connection The connection or null
*/
public Connection getConnection(String name,String url,
String username,String password) throws SQLException {
if (!clients.containsKey(name))
{
createPools(name,url,username,password,0);
clients.put(name,new Integer(0));
}

int iCurr = ((Integer)clients.get(name)).intValue();
iCurr++;
clients.put(name,new Integer(iCurr));

DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}

/**
* Closes all open connections and deregisters all drivers.
*/
public synchronized void release(String name) throws SQLException {
// Wait until called by the last client
if (!clients.containsKey(name)) return;

int iCurr = ((Integer)clients.get(name)).intValue();
iCurr--;
clients.put(name,new Integer(iCurr));

if (iCurr > 0) return;
clients.remove(name);

DBConnectionPool pool = (DBConnectionPool)pools.get(name);
pool.release();

Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
DriverManager.deregisterDriver(driver);
}
}

private void createPools(String poolName,String url,
String username,String password,int max) {
DBConnectionPool pool =
new DBConnectionPool(poolName,url,username,password, max);
pools.put(poolName, pool);
log("Initialized pool " + poolName);
}

/**
* Loads properties and initializes the instance with its values.
*/
private void init() throws SQLException {
log = new PrintWriter(System.err);
loadDrivers();
//createPools(0);
}

/**
* Loads and registers all JDBC drivers. This is done by the
* DBConnectionManager, as opposed to the DBConnectionPool,
* since many pools may share the same driver.
*
* @param props The connection pool properties
*/
private void loadDrivers() throws SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
}

/**
* Writes a message to the log file.
*/
private void log(String msg) {
log.println(new Date() + ": " + msg);
}

/**
* Writes a message with an Exception to the log file.
*/
private void log(Throwable e, String msg) {
log.println(new Date() + ": " + msg);
e.printStackTrace(log);
}

/**
* This inner class represents a connection pool. It creates new
* connections on demand, up to a max number if specified.
* It also makes sure a connection is still open before it is
* returned to a client.
*/
class DBConnectionPool {
private int checkedOut;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;

/**
* Creates new connection pool.
*
* @param name The pool name
* @param URL The JDBC URL for the database
* @param user The database user, or null
* @param password The database user password, or null
* @param maxConn The maximal number of connections, or 0
* for no limit
*/
public DBConnectionPool(String name, String URL, String user,
String password,int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}

/**
* Checks in a connection to the pool. Notify other Threads that
* may be waiting for a connection.
*
* @param con The connection to check in
*/
public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}

/**
* Checks out a connection from the pool. If no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. If a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
*/
public synchronized Connection getConnection() throws SQLException {
Connection con = null;
if (freeConnections.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
if (con.isClosed()) {
log("Removed bad connection from " + name);
// Try again recursively
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}

/**
* Closes all available connections.
*/
public synchronized void release() throws SQLException {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
con.close();
}
freeConnections.removeAllElements();
}

/**
* Creates a new connection, using a userid and password
* if specified.
*/
private Connection newConnection() throws SQLException {
Connection con = null;
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log("Created a new connection in pool " + name);
return con;
}
}
}
应该网 2004-09-10
  • 打赏
  • 举报
回复
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class DBConnectionManager {
static private DBConnectionManager instance; // The single instance

private Vector drivers = new Vector();
private PrintWriter log;
private Hashtable pools = new Hashtable();

private Hashtable clients = new Hashtable();

/**
* A private constructor since this is a Singleton
*/
private DBConnectionManager()
throws SQLException {
init();
}

/**
* Returns the single instance, creating one if it's the
* first time this method is called.
*
* @return DBConnectionManager The single instance.
*/
public static synchronized DBConnectionManager getInstance()
throws SQLException
{
if (instance == null) {
instance = new DBConnectionManager();
}
return instance;
}

/**
* Returns a connection to the named pool.
*
* @param name The pool name as defined in the properties file
* @param con The Connection
*/
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}

guxinglou 2004-09-10
  • 打赏
  • 举报
回复
有的话,请发到这个邮箱:jlz@a-best.com.cn谢谢!

结贴时送分~~
guxinglou 2004-09-10
  • 打赏
  • 举报
回复
驱动包是:orai18n.jar,ojdbc14.jar,ojdbc14_g.jar,ojdbc14dms.jar,ojdbc14dms_g.jar,ocrs12.jar还是什么?

请指教!
yujiabian 2004-09-10
  • 打赏
  • 举报
回复
你如果想要的话,把你信箱给我!我给你发
yujiabian 2004-09-10
  • 打赏
  • 举报
回复
用odbc连接的效率很低的,
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orc";
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
这就是主要的,然后后面在写上
ResultSet rs=stmt.executeQuery("select * from table");

用rs.getString("字段名 ")获取 数据库值
haroyy 2004-09-10
  • 打赏
  • 举报
回复
最好别用ODBC!
guxinglou 2004-09-10
  • 打赏
  • 举报
回复
用ODBC连接数据库的代码呢?
haroyy 2004-09-10
  • 打赏
  • 举报
回复
记得把驱动包拷贝到jdk\jre\lib\ext下
leilu 2004-09-10
  • 打赏
  • 举报
回复
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orc";
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
加载代码。
guxinglou 2004-09-10
  • 打赏
  • 举报
回复
oracle 10从官方下载的。一下就搞定!
AHUA1001 2004-09-10
  • 打赏
  • 举报
回复
你的ORACLE 10是哪弄到的,能下载到吗?
JSP连ORACLE应该也是用CLASSES12.ZIP那个文件吧。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧