1 创建一个员工表emp自己添加员工数据 CREATE TABLE emp id INT PRIMARY KEY -- 员工id ename VARCHAR50 -- 员工姓名 job_id INT -- 职务id mgr INT -- 上级领导 Joindate Date -- 入职日期 salary double72 -- 工资 bonus double72 -- 奖金;
以下是使用jdbc预编译对象操作数据库的代码示例:
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JDBCExample {
public static void main(String[] args) {
// 数据库连接信息
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "password";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
conn = DriverManager.getConnection(url, username, password);
// 3. 修改1号数据的salary为10000
String sql1 = "UPDATE emp SET salary = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql1);
pstmt.setDouble(1, 10000);
pstmt.setInt(2, 1);
int rows1 = pstmt.executeUpdate();
System.out.println("修改记录数:" + rows1);
// 4. 添加一条记录
String sql2 = "INSERT INTO emp (id, ename, job_id, mgr, Joindate, salary, bonus) VALUES (?, ?, ?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, 2);
pstmt.setString(2, "John");
pstmt.setInt(3, 2);
pstmt.setInt(4, 1);
pstmt.setDate(5, new java.sql.Date(System.currentTimeMillis()));
pstmt.setDouble(6, 5000);
pstmt.setDouble(7, 1000);
int rows2 = pstmt.executeUpdate();
System.out.println("添加记录数:" + rows2);
// 5. 删除刚才添加的记录
String sql3 = "DELETE FROM emp WHERE id = ?";
pstmt = conn.prepareStatement(sql3);
pstmt.setInt(1, 2);
int rows3 = pstmt.executeUpdate();
System.out.println("删除记录数:" + rows3);
// 6. 查询id为1的记录,将其封装为Map集合
String sql4 = "SELECT * FROM emp WHERE id = ?";
pstmt = conn.prepareStatement(sql4);
pstmt.setInt(1, 1);
rs = pstmt.executeQuery();
if (rs.next()) {
Map<String, Object> map = new HashMap<>();
map.put("id", rs.getInt("id"));
map.put("ename", rs.getString("ename"));
map.put("job_id", rs.getInt("job_id"));
map.put("mgr", rs.getInt("mgr"));
map.put("Joindate", rs.getDate("Joindate"));
map.put("salary", rs.getDouble("salary"));
map.put("bonus", rs.getDouble("bonus"));
System.out.println(map);
}
// 7. 查询所有记录,将其封装为List
String sql5 = "SELECT * FROM emp";
pstmt = conn.prepareStatement(sql5);
rs = pstmt.executeQuery();
List<Map<String, Object>> list = new ArrayList<>();
while (rs.next()) {
Map<String, Object> map = new HashMap<>();
map.put("id", rs.getInt("id"));
map.put("ename", rs.getString("ename"));
map.put("job_id", rs.getInt("job_id"));
map.put("mgr", rs.getInt("mgr"));
map.put("Joindate", rs.getDate("Joindate"));
map.put("salary", rs.getDouble("salary"));
map.put("bonus", rs.getDouble("bonus"));
list.add(map);
}
System.out.println(list);
// 8. 查询所有记录,将其封装为emp对象的List集合
String sql6 = "SELECT * FROM emp";
pstmt = conn.prepareStatement(sql6);
rs = pstmt.executeQuery();
List<Emp> empList = new ArrayList<>();
while (rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt("id"));
emp.setEname(rs.getString("ename"));
emp.setJobId(rs.getInt("job_id"));
emp.setMgr(rs.getInt("mgr"));
emp.setJoindate(rs.getDate("Joindate"));
emp.setSalary(rs.getDouble("salary"));
emp.setBonus(rs.getDouble("bonus"));
empList.add(emp);
}
System.out.println(empList);
// 9. 查询总记录数
String sql7 = "SELECT COUNT(*) FROM emp";
pstmt = conn.prepareStatement(sql7);
rs = pstmt.executeQuery();
if (rs.next()) {
int count = rs.getInt(1);
System.out.println("总记录数:" + count);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 10. 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class Emp {
private int id;
private String ename;
private int jobId;
private int mgr;
private Date joindate;
private double salary;
private double bonus;
// 省略getters和setters
}
请注意替换数据库连接信息,并根据自己的需求修改SQL语句和数据模型
原文地址: http://www.cveoy.top/t/topic/hHnX 著作权归作者所有。请勿转载和采集!