用JSP如何连ORACLE数据库,要用ORACLE驱动,不用JDBC-ODBC桥 需要的资源:服务器上装一个oracle就有了oracle的文档里有JDBC驱动连接的一切代码及说明,明天copy一段代码给你 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.csdn.net/expert/topic/94/94959.shtm 给你一个较详细的beanpackage com.db.oracle;import java.sql.*;import java.io.*;import javax.sql.*;public class DB{ protected Connection m_sqlConn;// 数据库连接对象 protected Statement m_sqlStmt;// 语句对象 protected String m_strDriver; protected String m_strConURL; protected String m_strSqlUser; protected String m_strSqlPass; protected String m_strDatabase; protected String m_strServerIP; protected String m_strServerPort; protected String m_strCharSet; protected String m_strSql; public DB() { this.m_sqlConn = null; this.m_sqlStmt = null; this.m_strDriver = "oracle.jdbc.driver.OracleDriver"; this.m_strConURL = "jdbc:oracle:oci8:@"; this.m_strSqlUser = "youruid"; this.m_strSqlPass = "youruidpwd"; this.m_strDatabase = "yourdbname"; this.m_strServerIP = "yourIP"; this.m_strServerPort = "1521"; this.m_strCharSet = "GB2312"; this.m_strSql = ""; } public static void log(String p_strLog) { System.out.println(new java.util.Date() + ":" + p_strLog); } public Connection connectToDB() throws SQLException, java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException/*,javax.naming.NamingException*/ { try { m_strConURL += m_strDatabase; Class.forName(m_strDriver).newInstance(); m_sqlConn = DriverManager.getConnection(m_strConURL, m_strSqlUser, m_strSqlPass); m_sqlStmt = m_sqlConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch(java.lang.InstantiationException e) { log("DB.connectToDB error, InstantiationException:" + e.getMessage()); throw e; } catch(java.lang.IllegalAccessException e) { log("DB.connectToDB error, IllegalAccessException:" + e.getMessage()); throw e; } catch(java.lang.ClassNotFoundException e) { log("DB.connectToDB error, ClassNotFoundException:" + e.getMessage()); throw e; } catch(SQLException e) { log("DB.connectToDB error, SQL Exception:" + e.getMessage()); throw e; } finally { return m_sqlConn; } } public boolean closeStatement() throws NullPointerException, SQLException { boolean bReturn = true; try { m_sqlStmt.close(); m_sqlStmt = null; } catch(NullPointerException e) { log("DB.closeStatement error, DB的成员:m_sqlStmt是空值。:" + e.getMessage()); bReturn = false; throw e; } catch(SQLException e) { log("DB.closeStatement error, SQLException:" + e.getMessage()); bReturn = false; throw e; } finally { return bReturn; } } public boolean closeConnection() throws NullPointerException, SQLException { boolean bReturn = true; try { m_sqlConn.close(); m_sqlConn = null; } catch(NullPointerException e) { log("DB.closeConnection error, DB的成员:m_sqlConn是空值。:" + e.getMessage()); bReturn = false; throw e; } catch(SQLException e) { log("DB.closeConnection error, SQLException:" + e.getMessage()); bReturn = false; throw e; } finally { return bReturn; } } public boolean close() throws NullPointerException, SQLException { if (closeStatement() && closeConnection()) { return true; }else{ return false; } } public void setAutoCommit(boolean bAuto) throws NullPointerException, SQLException { try { m_sqlConn.setAutoCommit(bAuto); } catch(NullPointerException e) { log("DB.setAutoCommit error, DB的成员:m_sqlConn是空值。:" + e.getMessage()); throw e; } catch(SQLException e) { log("DB.setAutoCommit error, SQLException:" + e.getMessage()); throw e; } } public void commit() throws NullPointerException, SQLException { try { m_sqlConn.commit(); } catch(NullPointerException e) { log("DB.commit error, DB的成员:m_sqlConn是空值。:" + e.getMessage()); throw e; } catch(SQLException e) { log("DB.commit error, SQLException:" + e.getMessage()); throw e; } } public ResultSet executeQuery(String strSql) throws NullPointerException, SQLException { ResultSet setRet = null; try { m_strSql = strSql; setRet = m_sqlStmt.executeQuery(strSql); } catch(NullPointerException e) { log("DB.executeQuery error, DB的成员:m_sqlStmt是空值。:" + e.getMessage()); throw e; } catch(SQLException e) { log("DB.executeQuery error, SQLException:" + e.getMessage()); log("SQL语句是:" + strSql); throw e; } finally { return setRet; } } public int executeUpdate(String strSql) throws NullPointerException, SQLException { int iRet = 0; try { m_strSql = strSql; iRet = m_sqlStmt.executeUpdate(strSql); } catch(NullPointerException e) { iRet = -1; log("DB.executeUpdate error, DB的成员:m_sqlStmt是空值。:" + e.getMessage()); throw e; } catch(SQLException e) { iRet = -1; log("DB.executeUpdate error, SQLException:" + e.getMessage()); log("SQL语句是:" + strSql); throw e; } finally { return iRet; } } 请问支持JDK1.3的ORACLE驱动哪里有? 这个驱动是ORACLE8带的,那低版本的ORACLE好象不自带驱动吧!那哪里有呢? struts2 response.sendRedirect页面跳转到action后再到页面url能吧项目名后面的参数去掉吗? alert问题 怎么让局域网上的机子访问tomcat服务器 关于MyEclipse的JSEclipse插件的问题? 请教java如何操作xml文件 jsp 到 提交到servelet问题?谢谢 有关列表框赋值的问题 HiaHia,三个绿星星了,没得说,散分了! 如何页面中取得正在执行的本页面的文件名? Apache Tomcat/4.0.4能支持多少用户? 为什么applet&servlet不能都放在classes目录中 jsp如何实现模拟客户端发送请求?
package com.db.oracle;
import java.sql.*;
import java.io.*;
import javax.sql.*;
public class DB
{
protected Connection m_sqlConn;// 数据库连接对象
protected Statement m_sqlStmt;// 语句对象
protected String m_strDriver;
protected String m_strConURL;
protected String m_strSqlUser;
protected String m_strSqlPass;
protected String m_strDatabase;
protected String m_strServerIP;
protected String m_strServerPort;
protected String m_strCharSet;
protected String m_strSql;
public DB()
{
this.m_sqlConn = null;
this.m_sqlStmt = null;
this.m_strDriver = "oracle.jdbc.driver.OracleDriver";
this.m_strConURL = "jdbc:oracle:oci8:@";
this.m_strSqlUser = "youruid";
this.m_strSqlPass = "youruidpwd";
this.m_strDatabase = "yourdbname";
this.m_strServerIP = "yourIP";
this.m_strServerPort = "1521";
this.m_strCharSet = "GB2312"; this.m_strSql = "";
}
public static void log(String p_strLog)
{
System.out.println(new java.util.Date() + ":" + p_strLog);
}
public Connection connectToDB() throws SQLException, java.lang.ClassNotFoundException,
java.lang.IllegalAccessException, java.lang.InstantiationException/*,javax.naming.NamingException*/
{
try
{
m_strConURL += m_strDatabase;
Class.forName(m_strDriver).newInstance();
m_sqlConn = DriverManager.getConnection(m_strConURL, m_strSqlUser, m_strSqlPass);
m_sqlStmt = m_sqlConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch(java.lang.InstantiationException e)
{
log("DB.connectToDB error, InstantiationException:" + e.getMessage());
throw e;
}
catch(java.lang.IllegalAccessException e)
{
log("DB.connectToDB error, IllegalAccessException:" + e.getMessage());
throw e;
}
catch(java.lang.ClassNotFoundException e)
{
log("DB.connectToDB error, ClassNotFoundException:" + e.getMessage());
throw e;
}
catch(SQLException e)
{
log("DB.connectToDB error, SQL Exception:" + e.getMessage());
throw e;
}
finally
{
return m_sqlConn;
}
}
public boolean closeStatement() throws NullPointerException, SQLException
{
boolean bReturn = true;
try
{
m_sqlStmt.close();
m_sqlStmt = null;
}
catch(NullPointerException e)
{
log("DB.closeStatement error, DB的成员:m_sqlStmt是空值。:" + e.getMessage());
bReturn = false;
throw e;
}
catch(SQLException e)
{
log("DB.closeStatement error, SQLException:" + e.getMessage());
bReturn = false;
throw e;
}
finally
{
return bReturn;
}
}
public boolean closeConnection() throws NullPointerException, SQLException
{
boolean bReturn = true;
try
{
m_sqlConn.close();
m_sqlConn = null;
}
catch(NullPointerException e)
{
log("DB.closeConnection error, DB的成员:m_sqlConn是空值。:" + e.getMessage());
bReturn = false;
throw e;
}
catch(SQLException e)
{
log("DB.closeConnection error, SQLException:" + e.getMessage());
bReturn = false;
throw e;
}
finally
{
return bReturn;
}
}
public boolean close() throws NullPointerException, SQLException
{
if (closeStatement() && closeConnection())
{
return true;
}else{
return false;
}
}
public void setAutoCommit(boolean bAuto) throws NullPointerException, SQLException
{
try
{
m_sqlConn.setAutoCommit(bAuto);
}
catch(NullPointerException e)
{
log("DB.setAutoCommit error, DB的成员:m_sqlConn是空值。:" + e.getMessage());
throw e;
}
catch(SQLException e)
{
log("DB.setAutoCommit error, SQLException:" + e.getMessage());
throw e;
}
}
public void commit() throws NullPointerException, SQLException
{
try
{
m_sqlConn.commit();
}
catch(NullPointerException e)
{
log("DB.commit error, DB的成员:m_sqlConn是空值。:" + e.getMessage());
throw e;
}
catch(SQLException e)
{
log("DB.commit error, SQLException:" + e.getMessage());
throw e;
}
}
public ResultSet executeQuery(String strSql) throws NullPointerException, SQLException
{
ResultSet setRet = null;
try
{
m_strSql = strSql;
setRet = m_sqlStmt.executeQuery(strSql);
}
catch(NullPointerException e)
{
log("DB.executeQuery error, DB的成员:m_sqlStmt是空值。:" + e.getMessage());
throw e;
}
catch(SQLException e)
{
log("DB.executeQuery error, SQLException:" + e.getMessage());
log("SQL语句是:" + strSql);
throw e;
}
finally
{
return setRet;
}
}
public int executeUpdate(String strSql) throws NullPointerException, SQLException
{
int iRet = 0;
try
{
m_strSql = strSql;
iRet = m_sqlStmt.executeUpdate(strSql);
}
catch(NullPointerException e)
{
iRet = -1;
log("DB.executeUpdate error, DB的成员:m_sqlStmt是空值。:" + e.getMessage());
throw e;
}
catch(SQLException e)
{
iRet = -1;
log("DB.executeUpdate error, SQLException:" + e.getMessage());
log("SQL语句是:" + strSql);
throw e;
}
finally
{
return iRet;
}
}