package Test; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class JDBCdemo4_ResultSet { @Test public void testResultSet() throws Exception {//DQL查询语句 //1.注册驱动 //Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 String url = "jdbc:mysql:///test?useSSL=false"; String username = "root"; String password = "1234"; Connection conn = DriverManager.getConnection(url, username, password); //3.定义sql String sql="select * from test"; //4.获取statement对象 Statement stmt = conn.createStatement(); //5.执行sql ResultSet rs = stmt.executeQuery(sql); //6.处理结果,遍历rs中所有的数据 //6.1光标向下移动一行,并且判断当前行是否有数据 /*while (rs.next()){ //6.2获取数据 getXxx() int id = rs.getInt(1); String name = rs.getNString(2); double money = rs.getDouble(3); System.out.println(id); System.out.println(name); System.out.println(money); System.out.println("--------------------"); }*/ //第二种写法 while (rs.next()){ //6.2获取数据 getXxx() int id = rs.getInt("id"); String name = rs.getNString("name"); double money = rs.getDouble("money"); System.out.println(id); System.out.println(name); System.out.println(money); System.out.println("--------------------"); } //7.释放资源 rs.close(); stmt.close(); conn.close(); } }
标签:JDBC,String,rs,ResultSet,System,API,sql,println,out From: https://www.cnblogs.com/Karl-hut/p/17481442.html