基于前一篇,连接上数据库后,现在开始实现增删改查
增加字段
@Test
public void testInsert() {
try {
Connection conn = MySqlDbUtil.getConnect();
String sql = "insert into band(name,remark) values(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "19软工2班");
pstmt.setString(2, "这是备注信息");
pstmt.execute();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200501103803239.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
多创建几个字段,以便于观察
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200501103857287.png)
修改表
@Test
public void testUpdate() {
try {
Connection conn = MySqlDbUtil.getConnect();
String sql = "update band set name=?, remark=? where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "新班级名称");
pstmt.setString(2, "这是新的备注信息");
pstmt.setInt(3, 2);
pstmt.execute();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200501103931209.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
删除字段
@Test
public void testDelete() {
try {
Connection conn = MySqlDbUtil.getConnect();
String sql = "delete from band where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 3);
pstmt.execute();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200501105414851.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
查询全部字段
@Test
public void testFindAll() {
try {
Connection conn = MySqlDbUtil.getConnect();
String sql = "select * from band";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();//结果集,
while (rs.next()) {
Integer id = rs.getInt("id");
String name = rs.getString("name");
String remark = rs.getString("remark");
System.out.println("id =" + id + ", name =" + name + ", remark=" + remark);
}
rs.close();
pstmt.execute();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050110553656.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
查询单个字段
@Test
public void testFindById() {
try {
Connection conn = MySqlDbUtil.getConnect();
String sql = "select * from band where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,2);
ResultSet rs = pstmt.executeQuery();//结果集,
if (rs.next()) {
Integer id = rs.getInt("id");
String name = rs.getString("name");
String remark = rs.getString("remark");
System.out.println("id =" + id + ", name =" + name + ", remark=" + remark);
}
rs.close();
pstmt.execute();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050111031647.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
遇到的问题
我在添加字段的时候,有遇到报错问题,处理方法如下
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050111054236.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)
打开workbench修改里面就可以了
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200501110553807.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNDIyMzQ2OTM1,size_16,color_FFFFFF,t_70)