一,創(chuàng)建數(shù)據(jù)庫中的表
1.創(chuàng)建表的sql語句
create table student(
stuID int,
stuName varchar(20),
stuAge int,
stuAdress varchar(40)
)
2.在表中插入數(shù)據(jù)
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中連接數(shù)據(jù)庫實現(xiàn)增刪改查
1.連接數(shù)據(jù)庫
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.實現(xiàn)查詢
查詢
public void query() throws Exception{
//加載數(shù)據(jù)庫的驅動
Class.forName(driver);
//使用驅動管理器來獲得連接
Connection con = DriverManager.getConnection(url, username, password);
//使用Connection創(chuàng)建PreparedStatement預處理對象
String sql="select * from student";
PreparedStatement pstm = con.prepareStatement(sql);
//使用PreparedStatement對象執(zhí)行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.實現(xiàn)增加
@Test
public void increase() throws Exception{
//加載數(shù)據(jù)庫的驅動
Class.forName(driver);
//使用驅動管理器來獲得連接
Connection con = DriverManager.getConnection(url, username, password);
//使用Connection創(chuàng)建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.實現(xiàn)刪除文章來源:http://www.zghlxwxcb.cn/news/detail-511160.html
@Test
public void delete() throws Exception{
//加載數(shù)據(jù)庫的驅動
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.實現(xiàn)修改文章來源地址http://www.zghlxwxcb.cn/news/detail-511160.html
改
@Test
public void modify() throws Exception{
//加載數(shù)據(jù)庫的驅動
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連接數(shù)據(jù)庫實現(xiàn)增刪改查的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!