JavaJavaWeb 连接数据库完成增删改查
作者:mmseoamin日期:2023-12-25

在这里插入图片描述

🍖🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖

🍖🍖

🍖🍖🍖

🍖🍖🍖🍖 作者 : 不良使

🍖🍖🍖🍖🍖 潜力创作新星 华为云享专家

🍖🍖🍖🍖🍖🍖 博客记录学习的思路,项目和错误,寻找志同道合的朋友

🍖🍖🍖🍖🍖🍖🍖 如果觉得有帮助记得一键三连 ┗|`O′|┛ 嗷~~

🍖🍖🍖🍖🍖🍖🍖🍖

🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖🍖 🍖🍖🍖🍖🍖🍖🍖

连接数据库无非就是和数据库建立连接,和前面的python连接数据库差不多。


首先在连接Java数据库的时候需要数据库的jar包,这点不同于python连接数据库

mysql的jar包

下面让我们来看看数据库是如何让连接的,是如何完成数据库的一系列操作,增删改查等…


💖Java连接数据库。

🎶1、注册驱动

Class.forName("com.mysql.jdbc.Driver");

🎶2、获取连接

  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");

注意:获取连接很重要,数据的一点点错误都会导致你连接不上数据库。

😃1、localhost代表本机不用变

😃2、3306是端口号,即port,一般默认都是3306。

😃3、root一般指管理员,不用变,直接用即可。

😃4、"123456"指的是密码,一般大家常用的密码就123、111111、root或者123456几种。


🎶3、获取数据库操作对象

stmt = conn.createStatement();

🎶4、//执行sql

 String sql = "select id,name,num,price from testmodel_car";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String num = rs.getString("num");
                String price = rs.getString("price");
                System.out.println(id + "," + name + "," + num + "," + price);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

注意:

😃1、java"select id,name,num,price from testmodel_car";,从表 testmodel_car中查询 id,name,num,price四列数据

😃2、rs = stmt.executeQuery(sql);,获取的对象编译一下sql语句

😃3、String id = rs.getString("id");,取出id那一列的值赋值给id(String id 中的id可以随便换,只要不和其他变量冲突都是可以的)


🎶5、释放资源

       if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

不释放资源也行,在上面抛出异常也可以解决


🎶6、完整代码

import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test2 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");
            //获取数据库操作对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "select id,name,num,price from testmodel_car";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String num = rs.getString("num");
                String price = rs.getString("price");
                System.out.println(id + "," + name + "," + num + "," + price);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


💖连接数据库------增加操作

add.jsp

<%--
  Created by IntelliJ IDEA.
  User: 17331
  Date: 2022/3/5
  Time: 15:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>