数据库学习笔记02

Posted 夜色架构师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库学习笔记02相关的知识,希望对你有一定的参考价值。

使用JDBC连接数据库:

首先下载好JDBC连接的包:

链接:https://pan.baidu.com/s/1OmjCCbrTy6giJNjGJuMxEQ 
提取码:5qhp 
--来自百度网盘超级会员V3的分享

然后首先注册驱动:(注意mysql8.0版本以上加cj)

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

获取数据库连接:

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

3.定义 sql 语句

 String sql1  = .............

4.获取执行 sql 的对象

Statement stmt = conn.createStatement();

执行 sql 语句:

        int count1 = stmt.executeUpdate(sql1);
        int count2 = stmt.executeUpdate(sql2);

释放资源:

        stmt.close();
        conn.close();

全部代码:

package 任务十六__JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @author $范涛之
 * @Description
 * @create 2021-12-05 9:12
 */
public class Test 
    public static void main(String[] args) throws Exception 

        /**
         * 1:注册驱动
         */
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","root");

        String sql1= "CREATE TABLE employers_table(\\n" +
                "id INTEGER(8) NOT NULL AUTO_INCREMENT,\\n" +
                "department_id INTEGER(8) NOT NULL,\\n" +
                "emp_name CHAR(16) NOT NULL,\\n" +
                "gender CHAR(8),\\n" +
                "age INTEGER(8),\\n" +
                "join_date CHAR(32),\\n" +
                "PRIMARY KEY(id)\\n" +
                ");";

        String sql2 = "CREATE TABLE departments_table(\\n" +
                "department_id\\tINTEGER(4) NOT NULL AUTO_INCREMENT,\\n" +
                "department_name\\tCHAR(16)   NOT NULL,\\n" +
                "PRIMARY KEY(department_id)\\n" +
                ");";


        Statement statement = connection.createStatement();
     statement.executeUpdate(sql1);
     statement.executeUpdate(sql2);
    

        statement.close();
        connection.close();

    


运行结果:


:

插入十万条数据:

package 任务十六__JDBC;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @author $范涛之
 * @Description
 * @create 2021-12-05 15:53
 */
public class Test3 
    public static void main(String[] args) throws  Exception 
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/csdn", "root", "root");
        String sql1 = "INSERT INTO employee (dpmid,empname,age)\\n" +
                      "VALUES\\n" +
                      "(1,SUBSTRING(MD5(RAND()),1,20),FLOOR(RAND()*80));";
        Statement statement = connection.createStatement();

        int num = statement.executeUpdate(sql1);

        long startTime = System.currentTimeMillis();

        for (int i = 1; i < 10000; i++) 
            String sql = "INSERT INTO employee (dpmid,empname,age)\\n" +
                    "VALUES\\n" +
                    "(0,SUBSTRING(MD5(RAND()),1,20),FLOOR(RAND()*80));";
            num += statement.executeUpdate(sql);

        
        long endTime = System.currentTimeMillis();
        System.out.println("一共添加了"+num+"条数据");
        System.out.println("耗时:"+(endTime-startTime));
        statement.close();
        connection.close();






    


结果:

批处理:

package 任务十六__JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @author $范涛之
 * @Description
 * @create 2021-12-05 16:07
 */
public class Test4 
    public static void main(String[] args) throws Exception 
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/csdn", "root", "root");
        Statement statement = connection.createStatement();
        long startTime = System.currentTimeMillis();

        for (int i = 1; i < 10000; i++) 
            String sql = "INSERT INTO employee (dpmid,empname,age)\\n" +
                    "VALUES\\n" +
                    "(0,SUBSTRING(MD5(RAND()),1,20),FLOOR(RAND()*80));";
            statement.addBatch(sql);
            statement.executeBatch();
        
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:" + (endTime - startTime));
        statement.close();
        connection.close();


    


结果:

以上是关于数据库学习笔记02的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记 链接

AngularJS入门学习笔记一

AngularJS的学习笔记

ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

DOM探索之基础详解——学习笔记

ReactJs学习笔记01