package test0813;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test02 {
public void doSelect(int Id) {
// 根据主键查询
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8",
"root", "root");
PreparedStatement pst = con.prepareStatement("select * from dept where Id=?");
pst.setInt(1, Id);// 给第一个问号传了值
ResultSet rs = pst.executeQuery();
if (rs.next()) {
// 如果有下一行才输出,不会报空指针异常
System.out.println(rs.getInt("Id") + "\t" + rs.getString("dname") + "\t" +
eclipse连接数据库 进行增删改查操作
最新推荐文章于 2024-04-25 14:30:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)