JDBC简介(ResultSet接口)

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();
	}

}

你可能感兴趣的:(JDBC简介,resultset,jdbc)