zoukankan      html  css  js  c++  java
  • 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") + "	" +
                                    resultSet.getString("s_name") + "	" +
                                    resultSet.getString("s_sex") + "	" +
                                    resultSet.getDate("s_birthday") + "	" +
                                    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
    " +
                    "(
    " +
                    "    s_no int(8) not null primary key auto_increment,
    " +
                    "    s_name varchar(12),
    " +
                    "    s_sex varchar(4),
    " +
                    "    s_score double(6,1)
    " +
                    ")");
    
            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(
    " +
                    "	id int not null primary key auto_increment,
    " +
                    "	name varchar(20),
    " +
                    "	money DOUBLE(10,2)
    " +
                    ")");
            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)
    " +
                    "values
    " +
                    "('张三',10000),
    " +
                    "('李四',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();
        }
    
    }
    
    

    运行结果

    参考网址

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

  • 相关阅读:
    Python模块介绍及常见报错
    Vue入门及基本使用
    仿黑客帝国片头文字流星雨
    CSS3之flex布局演示
    京东商城延迟加载
    python第四次学习笔记
    python第二次学习笔记
    今天的第一个程序
    python第一次学习笔记(可能会有更新)
    ios导航条透明
  • 原文地址:https://www.cnblogs.com/d534/p/15243477.html
Copyright © 2011-2022 走看看