while(rs.next())执行不了
执行while(rs.next())进不了。语句如下:packagemysql01;importjava.sql.*;publicclassMySqlConnector{...
执行while(rs.next())进不了。
语句如下:
package mysql01;
import java.sql.*;public class MySqlConnector { //加载java连接mysql驱动 public static final String DBDRIVER = "com.mysql.jdbc.Driver"; //声明mysql的链接、用户名和密码(密码为空) static String URL ="jdbc:mysql://localhost/hanxg"; static String USER ="root"; static String PASSWORD = "654321"; public static void main(String[] args) { // TODO Auto-generated method stub java.sql.Connection conn = null; //String name ="13400555"; try { Class.forName(DBDRIVER); } catch(ClassNotFoundException e){ e.printStackTrace(); } try{ //建立sql连接 conn = DriverManager.getConnection(URL,USER,PASSWORD); //执行sql语句 Statement stmt = conn.createStatement(); String sql = "select * from name_password"; ResultSet rs = stmt.executeQuery(sql); System.out.println(rs.next()); while(rs.next()){ System.out.println("rs有值"); } rs.close(); stmt.close(); } catch (SQLException e){ e.printStackTrace(); } try{ conn.close(); }catch (SQLException e){ e.printStackTrace(); } }}
执行到 System.out.println(rs.next()); 可以打印出true(即可以执行到表示执行到查询语言都是正常的,而且是有结果的)
但是while(rs.next()){ System.out.println("rs有值"); }
不能执行,请高手指点啊,跪求 展开
语句如下:
package mysql01;
import java.sql.*;public class MySqlConnector { //加载java连接mysql驱动 public static final String DBDRIVER = "com.mysql.jdbc.Driver"; //声明mysql的链接、用户名和密码(密码为空) static String URL ="jdbc:mysql://localhost/hanxg"; static String USER ="root"; static String PASSWORD = "654321"; public static void main(String[] args) { // TODO Auto-generated method stub java.sql.Connection conn = null; //String name ="13400555"; try { Class.forName(DBDRIVER); } catch(ClassNotFoundException e){ e.printStackTrace(); } try{ //建立sql连接 conn = DriverManager.getConnection(URL,USER,PASSWORD); //执行sql语句 Statement stmt = conn.createStatement(); String sql = "select * from name_password"; ResultSet rs = stmt.executeQuery(sql); System.out.println(rs.next()); while(rs.next()){ System.out.println("rs有值"); } rs.close(); stmt.close(); } catch (SQLException e){ e.printStackTrace(); } try{ conn.close(); }catch (SQLException e){ e.printStackTrace(); } }}
执行到 System.out.println(rs.next()); 可以打印出true(即可以执行到表示执行到查询语言都是正常的,而且是有结果的)
但是while(rs.next()){ System.out.println("rs有值"); }
不能执行,请高手指点啊,跪求 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询