欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC简介(ResultSet接口)

程序员文章站 2022-06-15 12:17:42
...

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接口)
相关标签: resultset jdbc