注意:其中的JdbcUtil是我自定义的连接工具类:
package day02_statement;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import util.JdbcUtil;/** * 使用statement执行DQL语句: * select * @author mzy * */public class Demo04 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select * from employee"; try { conn = JdbcUtil.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()) { // 可以通过下标取值,但是不推荐,下标取值,字段多了不好 // 同样可以getString、getInt System.out.println( ""+rs.getObject("id")+""+ ""+rs.getObject("name")+", "+ ""+rs.getObject("gender")+", "+ ""+rs.getObject("age")+", "+ ""+rs.getObject("title")+", "+ ""+rs.getObject("email")+", "+ ""+rs.getObject("phone") ); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.close(rs, stmt, conn); } }}