Dbconn.javars.next()循环没有在Oracle数据库数据检索
import com.OracleCon;
public class Dbconn{
public static void main(String[] args){
try{
OracleCon oraclecon = new OracleCon();
oraclecon.callmethod();
}catch(Exception e){
System.out.println("Ex e"+e);
}
}
}
OracleCon.java
package com;
import java.lang.NullPointerException;
import java.sql.*;
public class OracleCon{
String JDBC_DRIVER="oracle.jdbc.driver.OracleDriver";
String db_URL="jdbc:oracle:thin:@localhost:1521:ORCL";
String username="SYSTEM";
String password="root";
Connection conn = null;
public OracleCon() {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection(db_URL,username,password);
if(conn!=null){
System.out.println("Database connected");
}else{
System.out.println("Problem in connecting datbase");
}
}catch(SQLException se){
System.out.println("Error occured " + se.toString());
}catch(Exception e){
e.printStackTrace();
}
}
public void callmethod() throws SQLException {
PreparedStatement statement = null;
conn = DriverManager.getConnection(db_URL,username,password);
try{
Statement stmt = null;
stmt = conn.createStatement();
String sql = "select USERID from userdetails";
System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Result set finished "+rs);
while(rs.next()){
System.out.println(rs.getString("USERID"));
}
rs.close();
}catch(SQLException se){
System.out.println("SQL ex"+se);
}catch(Exception e){
System.out.println("Ex e"+e);
}
finally {
closeConnection();
}
}
public void closeConnection(){
try{
if(conn!=null){
conn.close();
}
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
该数据库被成功连接,并且RS被保持一些目标函数这由rs.next()上面的Sysout语句显示。但问题是它没有输入while(rs.next()),我试图通过在该循环内打印一些名称,即使该Sysout不打印。我不知道我的代码有什么问题。它适用于Mysql,但它不适用于Oracle数据库。
也许,你在不同的数据库会话中插入记录,并忘记'提交'它? – 2014-09-13 09:15:42