学习IDEA连接MySQL数据库(实现增删改查)1、在测试类testDbUtil.java编写测试⽅法:
增加:
ie开启javascript
@Test
public void testInsert(){
try{
Connection conn = Connection();
String sql ="INSERT INTO band(name,remark) VALUES(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理
pstmt.setString(1,"软⼯1班");
pstmt.setString(2,"备注信息");
int result = uteUpdate();
if(result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
在运⾏代码前这是之前添加的两条记录:
运⾏后,添加了跳记录:
修改(按i条件为d进⾏修改):
@Test
public void testUpdate(){
try{distinct 编程语句
Connection conn = Connection();
String sql ="UPDATE band set name=?,remark=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理        pstmt.setString(1,"软⼯2班");
pstmt.setString(2,"备注信息2");
pstmt.setInt(3,5);
int result = uteUpdate();
if(result>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
运⾏代码后id为5的记录被修改了:
删除(按条件id进⾏删除):
@Test
public void testDelete(){
try{
Connection conn = Connection();
String sql ="DELETE FROM band WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理            pstmt.setInt(1,5);
int result = uteUpdate();
if(result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
运⾏代码后id为5的记录被删除了:
查询(查询全部记录):
@Test
public void testFindAll(){
try{
Connection conn = Connection();
String sql ="SELECT * FROM band";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理          ResultSet rs = uteQuery();
()){
int id = rs.getInt("id");
String name = rs.getString("name");
String  remark = rs.getString("remark");
System.out.println(id+","+name+","+remark);
}
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
运⾏代码后我们可以看到全部记录:
查询(按id进⾏查询):
@Test
public void testFindById(){
try{
Connection conn = Connection();
String sql ="SELECT * FROM band WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理          pstmt.setInt(1,1);
ResultSet rs = uteQuery();
()){win10python安装教程
int id = rs.getInt("id");maven依赖排除
String name = rs.getString("name");
String  remark = rs.getString("remark");
System.out.println(id+","+name+","+remark);
}
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
查询id=1,运⾏代码后如下:
mysql菜鸟教程增删改查
查询(按通配字符%惊醒查询):
@Test
public void testFindByName(){
try{
Connection conn = Connection();
String sql ="SELECT * FROM band WHERE name LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);//sql语句预处理            pstmt.setString(1,"%2%");
ResultSet rs = uteQuery();
()){
int id = rs.getInt("id");
String name = rs.getString("name");
String  remark = rs.getString("remark");
System.out.println(id+","+name+","+remark);
}
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
>java事务隔离的四个级别

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。