1 package org.west.demo3; 2 3 import java.sql.*; 4 5 public class MyTest { 6 public static void main(String[] args) throws ClassNotFoundException, SQLException { 7 Class.forName("com.mysql.jdbc.Driver"); 8 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcstudy", "root", "123456"); 9 10 String sql="select * from t_user where id>?"; 11 PreparedStatement ps = connection.prepareStatement(sql); 12 ps.setString(1,"2"); 13 ResultSet rs= ps.executeQuery(); 14 while(rs.next()){ 15 System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"-------"+rs.getString(3)); 16 } 17 } 18 }
resultset.getString()
返回所对应的字段 比如你的表中字段为id,name,password
rs.getString(1)就是返回id列的值
rs.getString(2)就是返回name列的值
或者也可以写成rs.getString("id");
rs.getString("name");