第一个类
- Java code
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();
------解决方案--------------------
- Java code
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(); } }}