JDBC簡介(ResultSet介面)
阿新 • • 發佈:2019-02-05
在ResultSet介面定義瞭如下的方法:
·向下移動指標判斷是否有資料行:public boolean next()throws SQLException;
·取出資料內容:getInt(), getDouble(), getString() ,getDate()
範例:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Date; public class OracleDemo { private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:acehzh"; private static final String USER = "scott"; private static final String PASSWORD = "tiger"; public static void main(String[] args) throws Exception { // 1.載入驅動 Class.forName(DBDRIVER); // 2.連線資料庫 Connection conn = DriverManager.getConnection(DBURL, USER, PASSWORD); // 3.操作資料庫 Statement sta = conn.createStatement(); String sql = "SELECT mid,name,birthday,age,note " + " FROM member"; ResultSet rs = sta.executeQuery(sql); while (rs.next()) { int mid = rs.getInt(1); String name = rs.getString(2); Date birthday = rs.getDate(3); int age = rs.getInt(4); String note = rs.getString(5); System.out.println(mid + "," + name + "," + birthday + "," + age + "," + note); } rs.close(); conn.close(); } }