目录
(2)在idea中书写代码将mysql与java连接,实现基本的增删改查
1.通过connector连接Java和Mysql数据库
(1)首先配置idea
我们此处用的数据库是Mysql8.0版本,注意8.0版本的connector要用8.0,用5.0的后面会报错,下载网址:MySQL :: Download Connector/J 下载完成后点击加号,配置到idea中,配置完成后,idea的externLibrary中会有新添加的类包
我们的初步任务就完成啦!
(2)如何把java和mysql连接起来
1.简单连接
String url = "jdbc:mysql://localhost:3306/jdbc?useSSL=false";
String user = "root";
String password = "18342003" ;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url,user, password);
}catch(SQLEXCEPTION ex){
ex.printStackTrace();
}finally{
try {
con.close();
st.close();
rs.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
通过这样的代码我们可以实现简单的java与mysql的连接,需要注意url = jdbc:mysql://主机名:域名/数据库名称 user = 用户名 password = 密码 有mysql基础的同学应该很容易理解,finally我们关闭连接。这样实现了简单的连接
连接后我们可以写几个sql语句检验是
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url,user, password);
String sql_insert = "insert into student values(10,'aa',30)";
String sql_insert1 = "insert into student values(11,'aa',30)";
String sql_insert2 = "insert into student values(12,'aa',30)";
String sql_select = "select * from student";
//执行sql语句
st.executeUpdate(sql_insert);
st.executeUpdate(sql_insert1);
st.executeUpdate(sql_insert2);
while(rs.next()){
System.out.println(rs.getInt(1)+","+
rs.getString(2)+","+
rs.getInt(3)
);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
con.close();
st.close();
rs.close();
}catch(Exception ex){
ex.printStackTrace();
}
2.认识PrepareStatement
但是具体在添加数据库中,我们书写sql语句不可能是死的,肯定变化的,所以我们有着PrepareStatement用来写变化的sql语句 具体内容如下
PreparedStatement preparedStatement = con.prepareStatement("insert into student values(?,?,?)");
preparedStatement.setInt(1,3);
preparedStatement.setString(2,"abc");
preparedStatement.setInt(3,20);
preparedStatement.executeUpdate();
2.实现简单的图书管理系统
(1)创建数据库jdbc,并且创建出book表
可以在cmd命令行中输入create database jdbc;,也可以在navicat中直接创建
(2)在idea中书写代码将mysql与java连接,实现基本的增删改查
1.JdbcUtiles类,一个使用jdbc的工具类
package myDatebases_Demo;
import java.util.*;
import java.sql.*;
public class JdbcUtil {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/jdbc?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "18342003";
//注册驱动程序放在代码块中,每次只能注册一次
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//创建getConnection对象,用来获得connection对象
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
}catch (SQLException e){
e.printStackTrace();
}
return conn;
}
//创建free方法实现关闭连接功能
public static void close(Statement st, Connection conn) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public static void close(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
这个类在任何系统中都可以使用,是一个泛用性的,可以直接复制粘贴,毕竟java是一个面向复制
的编程语言
2.BookDb类,实现增删改查
1.增
public static void addBook(Book s) {
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("insert into book values (?,?,?)");
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setString(3, s.getPress());
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
2.删
//根据书本id删除书本信息
public static void delectBook(int id){
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("delete from book where id = ?");
pst.setString(1, String.valueOf(id));
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
3.改
//根据书本id更新书本信息
public static void updateBook(Book b, int id) {
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("update book set id = ?,name = ?,press = ? where id = ? ");
pst.setInt(1, b.getId());
pst.setString(2, b.getName());
pst.setString(3, b.getPress());
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
4.查
//根据书本id查询书本的信息
//保存在一个对象中,用于集中输出,或者放在集合中,用于管理输出等
public static Book findBookById(int id) {
Book b = new Book();
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from book where id = ?");
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
b.setId(rs.getInt(1));
b.setName(rs.getString(2));
b.setPress(rs.getString(3));
}
JdbcUtil.close(rs, pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
return b;
}
将信息都放在一个对象容器中,输出时用对象容器进行输出,因为我们具体在mysql中查询一个表的信息时,表的每一类是一个字段,而每一行正好可以对应java中的一个对象,所以我们可以把查到的所有信息都放在一个对象中,代表我们表的每一行
5.显示所有信息
//查询书本的所有信息,保存在一个集合中,后来再通过集合再把所有结果输出出来,避免此中语句过于冗杂,利于以后的更改
public static ArrayList<Book> queryBook() {
ArrayList<Book> list = new ArrayList<>();
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from book");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Book b = new Book();
b.setId(rs.getInt(1));
b.setName(rs.getString(2));
b.setPress(rs.getString(3));
list.add(b);
}
JdbcUtil.close(rs,pst,conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
return list;
}
此处便更好理解了,将所得到的每一行都放在list容器中,再输出。
全部代码:
package myDatebases_Demo;
import java.sql.*;
import java.util.ArrayList;
public class BookDb {
public static void addBook(Book s) {
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("insert into book values (?,?,?)");
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setString(3, s.getPress());
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//根据书本id删除书本信息
public static void delectBook(int id){
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("delete from book where id = ?");
pst.setString(1, String.valueOf(id));
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//根据书本id更新书本信息
public static void updateBook(Book b, int id) {
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("update book set id = ?,name = ?,press = ? where id = ? ");
pst.setInt(1, b.getId());
pst.setString(2, b.getName());
pst.setString(3, b.getPress());
pst.executeUpdate();
JdbcUtil.close(pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//根据书本id查询书本的信息
//保存在一个对象中,用于集中输出,或者放在集合中,用于管理输出等
public static Book findBookById(int id) {
Book b = new Book();
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from book where id = ?");
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
b.setId(rs.getInt(1));
b.setName(rs.getString(2));
b.setPress(rs.getString(3));
}
JdbcUtil.close(rs, pst, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
return b;
}
//查询书本的所有信息,保存在一个集合中,后来再通过集合再把所有结果输出出来,避免此中语句过于冗杂,利于以后的更改
public static ArrayList<Book> queryBook() {
ArrayList<Book> list = new ArrayList<>();
try {
Connection conn = JdbcUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from book");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Book b = new Book();
b.setId(rs.getInt(1));
b.setName(rs.getString(2));
b.setPress(rs.getString(3));
list.add(b);
}
JdbcUtil.close(rs,pst,conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
return list;
}
}
3.testMain类,运行处
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
public class testMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
Meau();
int choose = sc.nextInt();
switch (choose) {
case 1:
Book b = getBook();
BookDb.addBook(b);
//看了好多文章,好像都无法实现清空控制台的功能,这个是最实用的
break;
case 2:
System.out.println("请输入想要删除的书本的id");
int id = sc.nextInt();
BookDb.delectBook(id);
break;
case 3:
System.out.println("请输入想要更新的书本的id");
Book b1 = getBook();
int id1 = sc.nextInt();
BookDb.updateBook(b1,id1);
break;
case 4:
System.out.println("请输入想要查询的书本的id");
int id2 = sc.nextInt();
Book b2 = BookDb.findBookById(id2);
System.out.println("id\t\t name\t\t press\t");
System.out.println(b2.getId()+"\t\t"+b2.getName()+"\t\t"+b2.getPress());
break;
case 5:
System.out.println(BookDb.queryBook().size());
queryBook(BookDb.queryBook());
break;
}
}
}
public static void queryBook(ArrayList<Book> list){
System.out.println("id\t\t name\t\t press\t");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getId()+"\t\t"+list.get(i).getName()+"\t\t"+list.get(i).getPress()+"\t");
}
}
public static Book getBook(){
Book b = new Book();
Scanner sc = new Scanner(System.in);
System.out.println("请输入书本id:");
b.setId(sc.nextInt());
System.out.println("请输入书本名称:");
b.setName(sc.next());
System.out.println("请输入书本出版社名称: ");
b.setPress(sc.next());
return b;
}
public static void Meau(){
System.out.println("欢迎进入图书馆管理系统");
System.out.println("1 增加");
System.out.println("2 删除");
System.out.println("3 修改");
System.out.println("4 查询");
System.out.println("5 显示全部信息");
System.out.println("6 退出");
}
}
看一下效果
嘿嘿,这样我们就完成啦!!