日期:2014-05-18  浏览次数:20886 次

用bean作Oracle数据库访问总是失败,求助!
我打算用bean访问Oracle数据库,但是当运行到返回结果集的时候或者提交update命令的时候,就报出以下错误,请各位老哥老姐支援!:
程序:
package address;

import java.io.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class detector extends Object 
{

public detector(){
}

  public void saveaddr(HttpServletRequest request, HttpServletResponse response)
  throws ServletException,IOException
  {
Connection conn;
Statement stmt;
  String Stimestamp = "";
  String Saddress = "";
  String SRuser = "";
  String SRhost = "";
  String SRURI = "";
String sql = "";
PrintWriter out = response.getWriter();

  try {

out.println("102");


Class.forName("oracle.jdbc.driver.OracleDriver"); 
conn= DriverManager.getConnection("jdbc:oracle:thin:@172.30.253.107:1521:lvrtest","kbadmin","jhl1000ml");  


if (conn == null)
{
out.println("1009");
}else
{
out.println("1008");
}

stmt = conn.createStatement();  


if (stmt == null)
{
out.println("1009");
}else
{
out.println("1008");
}


out.println("103");

sql="select to_char(sysdate,'yyyymmddhh24miss') || dbms_random.string('1',20 ) as ts from dual";  
ResultSet rs=stmt.executeQuery(sql); 
out.println("106");
Stimestamp = rs.getString(1);
//Stimestamp ="20071106153451TUHDLZZTCOLRLRVGOUMY";

  out.println("111");
  out.println(Stimestamp);
  out.println("105");
   
  Saddress = request.getRemoteAddr();
  SRuser = request.getRemoteUser();
  SRhost = request.getRemoteHost();
  SRURI = request.getRequestURI();
 
  sql="insert into tsnoop values('" + Stimestamp +"','" + Saddress + "')";  
out.println("107");
stmt.executeUpdate(sql);
   
  out.println("101");
   
  rs.close();
  stmt.close();
  conn.close();
   
  } catch (Exception e) {
  System.out.println(e.getMessage());
  }  
  }

}

页面显示到102 1008 1008 103 106


------解决方案--------------------
conn= DriverManager.getConnection( "jdbc:oracle:thin:@172.30.253.107:1521:lvrtest ", "kbadmin ", "jhl1000ml ");
你引号里面的3项最后怎么都有空格啊,故意的?
------解决方案--------------------
在Stimestamp=rs.getString(1); 前加一句rs.next();
或者改成
while(rs.next()){
Stimestamp=rs.getString(1);
}
------解决方案--------------------
做java程序,我觉得,您最好不要这样做,要做的好,可以采用其它的数据库处理框架啊,hiberante ,toplink ,等其它一些框架,应该很不错的呢,我的建议是提前学习他们,最好是多学习两个的啊,这样对你写程序有好处的啊.!
顶!