|
package com.rk.db.c_prepared;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.rk.db.utils.JDBCUtil;
/**
* 使用PreparedStatement执行Select语句
* @author RK
*/
public class Demo04
{
public static void main(String[] args)
{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
//1.获取连接
conn = JDBCUtil.getConnection();
//2.准备预编译的sql
String sql = "SELECT * FROM T_Persons";
//3.执行预编译sql语句(检查语法)
pstmt = conn.prepareStatement(sql);
//4.执行sql语句,得到返回结果
rs = pstmt.executeQuery();
//5.输出
while(rs.next())
{
int id = rs.getInt("Id");
String userName = rs.getString("UserName");
String pwd = rs.getString("Pwd");
System.out.println(id + "\t" + userName + "\t" + pwd);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//关闭资源
JDBCUtil.close(conn, pstmt, rs);
}
}
}
|
|
|
|
|
|
|