当前位置: 技术问答>java相关
jsp连oracle的问题,就要交工了,急!!!
来源: 互联网 发布时间:2015-07-26
本文导语: 是这样的,我在本机上已经做好程序了,是用access作数据库的,用javabean来连数据库的,现在要移植到服务器的oracle数据库上去,因此我改了连库的bean,如下: package news; import java.io.*; import java.sql.*; public class sql...
是这样的,我在本机上已经做好程序了,是用access作数据库的,用javabean来连数据库的,现在要移植到服务器的oracle数据库上去,因此我改了连库的bean,如下:
package news;
import java.io.*;
import java.sql.*;
public class sql_data {
String host = "202.108.2.1"; //主机地址
String port = "1521"; //端口号
String sid = "ciq"; //SID
String user = "news"; //用户名
String pass = "pass"; //密码
ResultSet rs = null; //查询结果
Connection conn = null;
Statement stmt = null;
public void connect() {
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, pass);
}
catch (Exception e) {
System.err.println("数据库连接出错!");
}
}
public void disConn() {
try {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
}
catch (Exception e) {
System.err.println("数据库关闭出错!");
}
}
public sql_data() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connect();
}
catch(Exception e) {
System.err.println("数据库驱动装载出错!");
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
return rs;
}
public void executeUpdate(String sql) {
try {
stmt = conn.createStatement();
stmt.executeQuery(sql);
conn.commit();
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
}
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);这句老是通过不了,改成stmt = conn.createStatement();以后能通过,但是得不到查询的结果。并且我执行插入操作executeQuery()也能通过,但是没有插入到数据库里面,请问这是怎么回事?
package news;
import java.io.*;
import java.sql.*;
public class sql_data {
String host = "202.108.2.1"; //主机地址
String port = "1521"; //端口号
String sid = "ciq"; //SID
String user = "news"; //用户名
String pass = "pass"; //密码
ResultSet rs = null; //查询结果
Connection conn = null;
Statement stmt = null;
public void connect() {
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, pass);
}
catch (Exception e) {
System.err.println("数据库连接出错!");
}
}
public void disConn() {
try {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
}
catch (Exception e) {
System.err.println("数据库关闭出错!");
}
}
public sql_data() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connect();
}
catch(Exception e) {
System.err.println("数据库驱动装载出错!");
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
return rs;
}
public void executeUpdate(String sql) {
try {
stmt = conn.createStatement();
stmt.executeQuery(sql);
conn.commit();
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
}
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);这句老是通过不了,改成stmt = conn.createStatement();以后能通过,但是得不到查询的结果。并且我执行插入操作executeQuery()也能通过,但是没有插入到数据库里面,请问这是怎么回事?
|
你用到的数据库连接的声明是隐性声明,很多时候不起作用是很平常的,我也遇到过。
试一试如下的声明方法(替代DriverManager.registerDriver())
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Driver error!!");
}
试一试如下的声明方法(替代DriverManager.registerDriver())
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Driver error!!");
}
|
try:
public void executeUpdate(String sql) {
try {
stmt = conn.createStatement();
//stmt.executeQuery(sql);
stmt.executeUpdate(sql) ;
conn.commit();
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
}
http://www.csdn.net/expert/topic/670/670716.xml?temp=.3571741
public void executeUpdate(String sql) {
try {
stmt = conn.createStatement();
//stmt.executeQuery(sql);
stmt.executeUpdate(sql) ;
conn.commit();
}
catch(SQLException ex) {
System.err.println("SQL语句执行错误");
}
}
http://www.csdn.net/expert/topic/670/670716.xml?temp=.3571741
|
先别换,试一个:
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, pass);
变成:
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid + ";SelectMethod=cursor", user, pass);
再试一下。
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, pass);
变成:
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid + ";SelectMethod=cursor", user, pass);
再试一下。
|
要用classes12.zip
|
要用classes12.zip
去找一个吧,
去找一个吧,