📄 resultsetselectdemo.java
字号:
package resultset;
import java.sql.*;
public class ResultSetSelectDemo {
public ResultSetSelectDemo() {
//定义数据库连接的驱动程序
String driver = "org.gjt.mm.mysql.Driver";
//定义MySQL数据库的连接地址
String url = "jdbc:mysql://localhost:3306/EmployeeEBKC10";
//声明连接类
Connection conn = null;
//employee数据表的选择SQL语句
String sql = "select * from employeeTable";
try {
//使用JDBC技术创建数据库连接
Class.forName(driver);
//使用DriverManager类的getConnection()方法建立连接
conn = DriverManager.getConnection(url, "root", "test");
//通过Statement查询记录,使返回数据集可以前后移动
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//取得返回的记录集
ResultSet rs = stmt.executeQuery(sql);
//移到第一行和显示内容
rs.first();
this.showCurrentRow(rs);
//移到最后一行和显示内容
rs.last();
this.showCurrentRow(rs);
//移到第6行和显示内容
rs.absolute(6);
this.showCurrentRow(rs);
//上移1行和显示内容
rs.previous();
this.showCurrentRow(rs);
//下移2行和显示内容
rs.next();
rs.next();
this.showCurrentRow(rs);
}catch(Exception e){
e.printStackTrace();
}
}
//显示当前记录的方法
public void showCurrentRow(ResultSet rs) throws Exception{
String employeeId = rs.getString("employeeId");
String departmentId = rs.getString("departmentId");
String employeeName = rs.getString("employeeName");
String phone = rs.getString("phone");
String address = rs.getString("address");
java.sql.Timestamp joinDate = rs.getTimestamp("joinDate");
java.sql.Timestamp resignDate = rs.getTimestamp("resignDate");
int employeeStatus = rs.getInt("employeeStatus");
System.out.println(employeeId + "\t" + departmentId + "\t" +
employeeName + "\t" + phone + "\t" +
address + "\t" + joinDate + "\t" +
resignDate + "\t" + employeeStatus);
}
public static void main(String[] args) {
ResultSetSelectDemo resultSetSelectDemo1 = new ResultSetSelectDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -