一,创建数据库中的表
1.创建表的sql语句
create table student(
stuID int,
stuName varchar(20),
stuAge int,
stuAdress varchar(40)
)
2.在表中插入数据
insert into student(stuID,stuName,stuAge,stuAdress) values("1","张三","18","河南")
insert into student(stuID,stuName,stuAge,stuAdress) values("2","小美","18","东北")
insert into student(stuID,stuName,stuAge,stuAdress) values("3","sim","18","英国")
insert into student(stuID,stuName,stuAge,stuAdress) values("4","sim","18","英国")
二.在idea中连接数据库实现增删改查
1.连接数据库
private String driver="com.mysql.cj.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/student";
private String username="root";
private String password="root";
2.实现查询
查询
public void query() throws Exception{
//加载数据库的驱动
Class.forName(driver);
//使用驱动管理器来获得连接
Connection con = DriverManager.getConnection(url, username, password);
//使用Connection创建PreparedStatement预处理对象
String sql="select * from student";
PreparedStatement pstm = con.prepareStatement(sql);
//使用PreparedStatement对象执行sql语句
ResultSet re = pstm.executeQuery();
//判断
while (re.next()) {
int stuId=re.getInt("stuID");
String stuName=re.getString("stuName");
int stuAge=re.getInt("stuAge");
String stuAdress=re.getString("stuAdress");
test01 student = new test01();
student.setStuId(stuId);
student.setStuName(stuName);
student.setStuAge(stuAge);
student.setStuAddress(stuAdress);
System.out.println(student);
}
//资源回收
if (re!=null){
re.close();
}
if (pstm!=null){
pstm.close();
}
if (con!=null){
con.close();
}
}
3.实现增加
@Test
public void increase() throws Exception{
//加载数据库的驱动
Class.forName(driver);
//使用驱动管理器来获得连接
Connection con = DriverManager.getConnection(url, username, password);
//使用Connection创建PreparedStatement预处理对象
String sql="insert into student(stuId,stuName,stuAge,stuAdress) values (?,?,?,?)";
PreparedStatement pstm = con.prepareStatement(sql);
test01 student = new test01();
student.setStuId(5);
student.setStuName("小明");
student.setStuAge(20);
student.setStuAddress("日本");
pstm.setObject(1,student.stuId());
pstm.setObject(2,student.stuName());
pstm.setObject(3,student.stuAge());
pstm.setObject(4,student.stuAdress());
int n=pstm.executeUpdate();
if (n>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
//资源回收
if (pstm!=null){
pstm.close();
}
if (con!=null){
con.close();
}
//
}
4.实现删除文章来源:https://www.toymoban.com/news/detail-511160.html
@Test
public void delete() throws Exception{
//加载数据库的驱动
Class.forName(driver);
//使用驱动管理器来获得连接
Connection con = DriverManager.getConnection(url, username, password);
//3.编写SQL语句
String sql="delete from student where stuId=?";
//4.预处理SQL语句
PreparedStatement pstm = con.prepareStatement(sql);
pstm.setInt(1,1);
int n=pstm.executeUpdate();
if(n>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
//8.关闭资源
if (pstm!=null){
pstm.close();
}
if (con!=null){
con.close();
}
}
5.实现修改文章来源地址https://www.toymoban.com/news/detail-511160.html
改
@Test
public void modify() throws Exception{
//加载数据库的驱动
Class.forName(driver);
//使用驱动管理器来获得连接
Connection con = DriverManager.getConnection(url, username, password);
//3.编写SQL语句
String sql="update student set stuName=?,stuAge=? where stuId=?";
//4.预处理SQL语句
PreparedStatement pstm = con.prepareStatement(sql);
pstm.setObject(1,"王子龙");
pstm.setObject(2,22);
pstm.setObject(3,2);
int n=pstm.executeUpdate();
if(n>0){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
//8.关闭资源
if (pstm!=null){
pstm.close();
}
if (con!=null){
con.close();
}
}
}
到了这里,关于idea连接数据库实现增删改查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!