本文内容
idea中连接数据库之后,实现增删改查
准备工作
首先连接好数据库,不会的可以参考下面帖子idea连接数据库教程
建立如下表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
book_num varchar(11) CHARACTER NOT NULL,
book_name varchar(50) CHARACTER NOT NULL,
author varchar(30) CHARACTER NOT NULL,
publisher varchar(50) CHARACTER NOT NULL,
price decimal(10, 0) NOT NULL,
PRIMARY KEY (book_num)
)
插入如下数据
insert into book values('b001','数据库','李添酒','添酒出版社',25)
常见错误及解决方法
将数据库对应版本的连接包导到项目即可
数据库名字输错
正文
在连接好数据库的基础上,打开对应的项目。
连接数据库
封装为函数节约代码量
public Connection con()
{
//MySQL驱动包名
String driver_Name = "com.mysql.jdbc.Driver";
//数据库连接地址 booknamage为数据库名
String URL = "jdbc:mysql://localhost:3306/bookmanage?serverTimezone=GMT";
//用户名
String USER_NAME = "root";
//密码
String PASSWORD = "123456";
//
Connection con = null;
try {
//加载驱动类
Class.forName(driver_Name);
//获取连接
con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
//System.out.println("加载数据库驱动成功");
} catch (SQLException e) {
e.printStackTrace();
// System.out.println("获取连接成功");
}
return con;
}
查询
public void select() {
try {
Connection con=con();
//数据库查询语句
String sql = "SELECT * from book";
Statement prst = con.createStatement();
ResultSet rs = prst.executeQuery(sql);
//System.out.println("成功");
while (rs.next()) {
System.out.println(" " + rs.getString("book_num")+rs.getString("book_name")
+rs.getString("author")+rs.getString("publisher")+rs.getString("price"));
}
rs.close();
prst.close();
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
插入
public void insert(String num,String name,String author,String publisher,double price) {
try {
Connection con=con();
//数据库查询语句
String sql = "insert book values (?,?,?,?,?)";//有几个列加几个问号
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setString(1,num);
prepareStatement.setString(2,name);
prepareStatement.setString(3,author);
prepareStatement.setString(4,publisher);
prepareStatement.setDouble(5,price);
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
修改
将boo2价格改为50
public void update() {
try {
Connection con=con();
//数据库查询语句
String sql = "update book set price =? where book_num=?";//
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setDouble(1,50);
prepareStatement.setString(2,"b002");
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
删除
删除b002这条数据
public void delete(String book_num) //book_num 代表删除条件
{
try {
Connection con=con();
//数据库查询语句
String sql = "delete from book where book_num=?";//
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setString(1,book_num);
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
类代码
/*
Created by IntelliJ IDEA.
User: Wang
Date: 2019/12/15
Time: 22:28
*/
import java.sql.*;
public class db {
public Connection con()
{
//MySQL驱动包名
String driver_Name = "com.mysql.jdbc.Driver";
//数据库连接地址 booknamage为数据库名
String URL = "jdbc:mysql://localhost:3306/bookmanage?serverTimezone=GMT";
//用户名
String USER_NAME = "root";
//密码
String PASSWORD = "123456";
//
Connection con = null;
try {
//加载驱动类
Class.forName(driver_Name);
//获取连接
con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
//System.out.println("加载数据库驱动成功");
} catch (SQLException e) {
e.printStackTrace();
// System.out.println("获取连接成功");
}
return con;
}
public void select() {
try {
Connection con=con();
//数据库查询语句
String sql = "SELECT * from book";
Statement prst = con.createStatement();
ResultSet rs = prst.executeQuery(sql);
//System.out.println("成功");
while (rs.next()) {
System.out.println(" " + rs.getString("book_num")+rs.getString("book_name")
+rs.getString("author")+rs.getString("publisher")+rs.getString("price"));
}
rs.close();
prst.close();
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
public void insert(String num,String name,String author,String publisher,double price) {
try {
Connection con=con();
//数据库查询语句
String sql = "insert book values (?,?,?,?,?)";//有几个列加几个问号
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setString(1,num);
prepareStatement.setString(2,name);
prepareStatement.setString(3,author);
prepareStatement.setString(4,publisher);
prepareStatement.setDouble(5,price);
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
public void update() {
try {
Connection con=con();
//数据库查询语句
String sql = "update book set price =? where book_num=?";//
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setDouble(1,50);
prepareStatement.setString(2,"b002");
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
public void delete(String book_num) //book_num 代表删除条件
{
try {
Connection con=con();
//数据库查询语句
String sql = "delete from book where book_num=?";//
PreparedStatement prepareStatement=con.prepareStatement(sql);
//指定问号的值,是什么类型就set什么类型
prepareStatement.setString(1,book_num);
prepareStatement.execute();
//prepareStatement.executeUpdate();
//System.out.println("成功");
} catch (SQLException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
public static void main(String []args)
{
db db=new db();
//db.insert("b002","如何讨富婆欢心","陈师傅","大学出版社",35);
db.update();
db.delete("b002");
db.select();
}
}
有问题欢迎留言,博主会尽力解决