当前位置: 技术问答>java相关
我的这个连接为什么会出错? 在线等待......
来源: 互联网 发布时间:2017-03-31
本文导语: //单独编译执行并没有问题,但在JSP中调用时报DO_OPEN错误. String _DBDriver="oracle.jdbc.driver.OracleDriver"; String _ConnStr="jdbc:oracle:oci8:@double"; Class.forName(_DBDriver); ...
//单独编译执行并没有问题,但在JSP中调用时报DO_OPEN错误.
String _DBDriver="oracle.jdbc.driver.OracleDriver";
String _ConnStr="jdbc:oracle:oci8:@double";
Class.forName(_DBDriver);
_conn=DriverManager.getConnection(_ConnStr,"system","manager");
//最后一行报错
String _DBDriver="oracle.jdbc.driver.OracleDriver";
String _ConnStr="jdbc:oracle:oci8:@double";
Class.forName(_DBDriver);
_conn=DriverManager.getConnection(_ConnStr,"system","manager");
//最后一行报错
|
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=439
|
以下是Oracle自己提供的例子,可以参考以下。
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.math.BigDecimal;
public class SQLDataExample
{
public static void main(String args []) throws Exception
{
// Connect
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ());
String url = "jdbc:oracle:oci8:@";
try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
// Connect to the database
OracleConnection conn = (OracleConnection)
DriverManager.getConnection (url, "scott", "tiger");
java.util.Dictionary map = conn.getTypeMap();
map.put("EMPLOYEE", Class.forName("EmployeeObj"));
// Create a Statement
Statement stmt = conn.createStatement ();
try
{
stmt.execute ("drop table EMPLOYEE_TABLE");
stmt.execute ("drop type EMPLOYEE");
}
catch (SQLException e)
{
// An error is raised if the table/type does not exist. Just ignore it.
}
// Create and populate tables
stmt.execute ("CREATE TYPE EMPLOYEE AS OBJECT(EmpName VARCHAR2(50),EmpNo INTEGER)");
stmt.execute ("CREATE TABLE EMPLOYEE_TABLE (ATTR1 EMPLOYEE)");
stmt.execute ("INSERT INTO EMPLOYEE_TABLE VALUES (EMPLOYEE('Susan Smith', 123))");
stmt.close();
// Create a SQLData object
EmployeeObj e = new EmployeeObj("SCOTT.EMPLOYEE", "George Jones", 456);
// Insert the SQLData object
PreparedStatement pstmt
= conn.prepareStatement ("insert into employee_table values (?)");
pstmt.setObject(1, e, OracleTypes.STRUCT);
pstmt.executeQuery();
System.out.println("insert done");
pstmt.close();
// Select now
Statement s = conn.createStatement();
OracleResultSet rs = (OracleResultSet)
s.executeQuery("select * from employee_table");
while(rs.next())
{
EmployeeObj ee = (EmployeeObj) rs.getObject(1);
System.out.println("EmpName: " + ee.empName + " EmpNo: " + ee.empNo);
}
rs.close();
s.close();
if (conn != null)
{
conn.close();
}
}
}
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.math.BigDecimal;
public class SQLDataExample
{
public static void main(String args []) throws Exception
{
// Connect
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ());
String url = "jdbc:oracle:oci8:@";
try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
// Connect to the database
OracleConnection conn = (OracleConnection)
DriverManager.getConnection (url, "scott", "tiger");
java.util.Dictionary map = conn.getTypeMap();
map.put("EMPLOYEE", Class.forName("EmployeeObj"));
// Create a Statement
Statement stmt = conn.createStatement ();
try
{
stmt.execute ("drop table EMPLOYEE_TABLE");
stmt.execute ("drop type EMPLOYEE");
}
catch (SQLException e)
{
// An error is raised if the table/type does not exist. Just ignore it.
}
// Create and populate tables
stmt.execute ("CREATE TYPE EMPLOYEE AS OBJECT(EmpName VARCHAR2(50),EmpNo INTEGER)");
stmt.execute ("CREATE TABLE EMPLOYEE_TABLE (ATTR1 EMPLOYEE)");
stmt.execute ("INSERT INTO EMPLOYEE_TABLE VALUES (EMPLOYEE('Susan Smith', 123))");
stmt.close();
// Create a SQLData object
EmployeeObj e = new EmployeeObj("SCOTT.EMPLOYEE", "George Jones", 456);
// Insert the SQLData object
PreparedStatement pstmt
= conn.prepareStatement ("insert into employee_table values (?)");
pstmt.setObject(1, e, OracleTypes.STRUCT);
pstmt.executeQuery();
System.out.println("insert done");
pstmt.close();
// Select now
Statement s = conn.createStatement();
OracleResultSet rs = (OracleResultSet)
s.executeQuery("select * from employee_table");
while(rs.next())
{
EmployeeObj ee = (EmployeeObj) rs.getObject(1);
System.out.println("EmpName: " + ee.empName + " EmpNo: " + ee.empNo);
}
rs.close();
s.close();
if (conn != null)
{
conn.close();
}
}
}