JDBC 测试连接数据库 实现数据的CRUD

JDBC 测试连接数据库 实现数据的CRUD

package com.xiang.lesson01;

import java.sql.*;
import java.text.ParseException;


public class DBTest {
    /*
    **建立连接的五大步骤:**

1. 加载(注册)数据库

2. 建立链接

3. 语句对象来执行SQL语句

4. 处理结果集 、返回结果

5. 关闭数据库、释放资源
     */
//    private static  String URL = "jdbc:mysql://localhost:3307/webapp1?serverTimezone=utf8mb4";
//    useSSL=false 安全连接;
    private static String URL = "jdbc:mysql://localhost:3307/webapp2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static String DriverClass = "com.mysql.cj.jdbc.Driver";
    private static String UserName = "webapp1";
    private static String PassWord = "webapp1";
    private static Connection connection = null;
    private static PreparedStatement statement = null;
    private static ResultSet resultSet = null;


    public static Connection getConnection() {
        try {
            Class.forName(DriverClass);
            connection = DriverManager.getConnection(URL, UserName, PassWord);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

//    public  void  closeRerource(Connection connection){
//        try {
//            connection.close();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }

    public static void closeRerource() {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 查询
     *
     * @param
     */

    public static void getStudent() {
        connection = getConnection();
        try {
            statement = connection.prepareStatement("select  * from student");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                System.out.println(
                        resultSet.getInt("s_id") + "\t" +
                                resultSet.getString("s_name") + "\t" +
                                resultSet.getString("s_sex") + "\t" +
                                resultSet.getDate("s_birthday") + "\t" +
                                resultSet.getInt("s_professional"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 新增
     *
     * @param
     */
    public static void insertStudent() throws SQLException, ParseException {
        connection = getConnection();
        statement = connection.prepareStatement("insert into student(s_name,s_sex,s_birthday,s_professional) values (?,?,?,?)");
        statement.setString(1, "小小");
        statement.setString(2, "女");
//        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
        statement.setDate(3, new Date(20210908));
        statement.setInt(4, 4);

        int update = statement.executeUpdate();

        System.out.println(update + "insert into ----->ok");
    }

    /**
     * 创建表
     *
     * @param
     * @throws SQLException
     */
    public static void createTable() throws SQLException {
        connection = getConnection();
        statement = connection.prepareStatement("drop table if exists stu");
        statement.executeUpdate();
        statement = connection.prepareStatement("create table stu\n" +
                "(\n" +
                "    s_no int(8) not null primary key auto_increment,\n" +
                "    s_name varchar(12),\n" +
                "    s_sex varchar(4),\n" +
                "    s_score double(6,1)\n" +
                ")");

        int res = statement.executeUpdate();
        System.out.println(res + "创建表ok");
    }

    /**
     * 创建表 account
     *
     * @param args
     * @throws SQLException
     * @throws ParseException
     */
    /*
   create table account(
	id int not null primary key auto_increment,
	name varchar(20),
	money DOUBLE(10,2)
);
insert into account(name,money)
values
('张三',10000),
('李四',10000);
     */
    public static void account() throws SQLException {
        connection = getConnection();
        statement = connection.prepareStatement("drop table if exists account");
        statement.executeUpdate();
        statement = connection.prepareStatement("create table account(\n" +
                "\tid int not null primary key auto_increment,\n" +
                "\tname varchar(20),\n" +
                "\tmoney DOUBLE(10,2)\n" +
                ")");
        int res = statement.executeUpdate();
        System.out.println(res + "创建 account 表ok");
    }

    /**
     * insert into
     *
     * @param args
     * @throws SQLException
     * @throws ParseException
     */
    public static void insertaccount() throws SQLException {
        connection = getConnection();
        statement = connection.prepareStatement("insert into account(name,money)\n" +
                "values\n" +
                "('张三',10000),\n" +
                "('李四',10000)");
        int update = statement.executeUpdate();

        System.out.println(update + "insert into  account ----->ok");
    }

    /**
     * 更新,实现转账 功能
     * @param args
     * @throws SQLException
     * @throws ParseException
     */
    public static void task() throws SQLException {
        connection = getConnection();
        statement = connection.prepareStatement("update account set money = money - 5000 where  name = '张三'");
        statement.executeUpdate();
        statement = connection.prepareStatement("update account set money = money + 5000 where  name = '李四'");
        statement.executeUpdate();
        int update = statement.executeUpdate();

        System.out.println(update + "update  account task ----->ok");
    }


    public static void main(String[] args) throws SQLException, ParseException {
//        connection = DBTest.getConnection();
//        if (connection != null){
//            System.out.println("连接成功");
//        }else {
//            System.out.println("连接失败");
//        }
        account();
        insertaccount();
        task();
        createTable();
        insertStudent();
        getStudent();
        closeRerource();
    }

}

运行结果

JDBC 测试连接数据库 实现数据的CRUD

参考网址

https://www.cnblogs.com/shanlei/p/14353574.html

上一篇:SQL只是CRUD?


下一篇:MySQL之crud