当前位置:网站首页>Basic usage of MySQL

Basic usage of MySQL

2022-06-11 06:01:00 Not bald

Basic knowledge of database

The database is based on the hard disk to persist the data it needs , The database management mode not only improves the data storage efficiency , And it improves the security of data at the storage level .

Mseql Basic data types and related statements

value type : int,bigint,tinint,mediumint
String type : char,varchar
The date type : datatime,data
DQL( Data query language ):select sentence
DML( Data operation language ): Add, delete and modify the data in the table insert,delete,update
wait .
view the database :show databases;
Using a database : use Database name ;
View database tables :show tables;
View table structure : desc Table name ;
Delete Library :drop database Database name ;

Use JDBC

Recently, I have been learning about the basic use of databases and related concepts , Including the use of idea And JDBC Connect to the database , And use idea Add, delete, modify and query the contents of the database .
Use JDBC The first step is to download jar package , Then introduce the jar The step of packaging into the project is crucial , Then there are six key steps
1. Registration drive
2. Get database connection
3. Get database operation object
4. perform SQL sentence
5. Process query results
6. Release resources
The following code takes a query as an example

import com.mysql.jdbc.Driver;

import java.sql.*;

public class jdbc {
    
    public static void main(String[] args) {
    
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
    
            DriverManager.registerDriver(new Driver());
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "root");
            stmt = conn.createStatement();
            String sql = "select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno0";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
    
                String ename = rs.getNString("ename");
                String dname = rs.getNString("dname");
                System.out.println(ename + "," + dname);
            }
        } catch (SQLException e) {
    
            e.printStackTrace();
        } finally {
    
            if (rs != null) {
    
                try {
    
                    rs.close();
                } catch (SQLException e) {
    
                    e.printStackTrace();
                }
                if (stmt != null) {
    
                    try {
    
                        stmt.close();
                    } catch (SQLException e) {
    
                        e.printStackTrace();
                    }
                    if (conn != null) {
    
                        try {
    
                            conn.close();
                        } catch (SQLException e) {
    
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

summary

After this period of study , I realized the difficulty of the back end , Learning is very miscellaneous , It's hard to understand , But there is no other way , Only hand ripe , come on. !

原网站

版权声明
本文为[Not bald]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020530437211.html