JAVA相关,while (super.rs.next()),总是进不了循环,求解,谢谢了
第一个类
package epet;import java.sql.*;public class Pet { protected Connection conn = null; protected PreparedStatement pstmt = null; protected ResultSet rs = null; //驱动地址 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; //连接字符串 private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:ORACLE10"; //打开连接 protected void open() { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(URL, "kzeson", "world"); System.out.println("连接成功!"); } catch (SQLException e) { e.printStackTrace(); } } //关闭连接 protected void closeAll() { try { if (rs != null) { rs.close(); } if(pstmt!=null){ pstmt.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } }}第2个类-------------------------package epet;import java.util.*;import java.sql.SQLException;public class PetManage extends Pet { Scanner input = new Scanner(System.in); public void printPet() { super.open(); String sql = "select id,name,age from tests"; try { super.pstmt = super.conn.prepareStatement(sql); super.rs = super.pstmt.executeQuery(); System.out.println("编号\t名字\t年龄"); System.out.println("------------------------------"); System.out.println(super.rs.next()); while (super.rs.next()) { System.out.println("wfwfwefwefwefw"); System.out.print(rs.getInt("id") + "\t"); System.out.print(rs.getString("name") + "\t"); System.out.println(rs.getInt("age") + "\t"); } } catch (SQLException e) { e.printStackTrace(); } finally { super.closeAll();
System.out.println(super.rs.next());这句话删掉。super都可以不要写的。当然也可以写试试下面的程序。import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;class Pet { protected Connection conn = null; protected Statement pstmt = null;//没有条件要填,就用Stament protected ResultSet rs = null; //驱动地址 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; //连接字符串 private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:ORACLE10"; //打开连接 protected void open() { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(URL, "kzeson", "world"); System.out.println("连接成功!"); } catch (SQLException e) { e.printStackTrace(); } } //关闭连接 protected void closeAll() { try { if (rs != null) { rs.close(); } if(pstmt!=null){ pstmt.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } }}public class PetManage extends Pet { Scanner input = new Scanner(System.in); public void printPet() { super.open(); String sql = "select id,name,age from tests"; try { super.pstmt = super.conn.createStatement(); super.rs = super.pstmt.executeQuery(sql); System.out.println("编号\t名字\t年龄"); System.out.println("------------------------------"); while (super.rs.next()){ System.out.println("wfwfwefwefwefw"); System.out.print(rs.getInt("id") + "\t"); System.out.print(rs.getString("name") + "\t"); System.out.println(rs.getInt("age") + "\t"); } } catch (SQLException e) { e.printStackTrace(); } finally { super.closeAll(); } }}
[解决办法]
System.out.println(super.rs.next());
这行不用写。
假如数据库里面暂时没有数据,那么 next() 就为空,就会直接停掉了。
next 语法就是判断下一行数据是否存在。
[解决办法]
明白了,你的rs被关闭了,你不能去调用创建连接的rs,你应该在第二个类里面创建rs去接受,数据库里面的数据,不然的话,这个rs是没有值的,如果里面没有值,当然就无法进入循环!
[解决办法]
在第二个类里面申明这个ResultSet rs;
然后直接rs = super.pstmt.executeQuery();
再来while(rs.next())
应该就能成功了