Using jar to connect and operate MySQL database

Keywords: Database MySQL SQL JDBC

Why use database

If you want to ask where the data comes from, it may be that no matter where Xiaobai has been working for many years, he will say in unison, of course, database. So why use database? Extract: database is a "record keeping system" (the definition emphasizes that database is a collection of several records). Also known as a database is "a collection of related data stored together in a certain organizational way to solve specific tasks" (the definition focuses on the organization of data). What's more, the database is called "a data warehouse". Of course, although the image of this statement, but not rigorous. Strictly speaking, database is "a warehouse that organizes, stores and manages data according to data structure". In the daily work of economic management, it is often necessary to put some relevant data into such a "warehouse" and deal with it according to the needs of management.

1, Create tables and save data in MySQL

Initially, it's easy to build a table, use InnoDB engine and utf-8 character set. Although they are all default, they are also best written.
create table student(
id int unsigned auto_increment primary key,
name char(20) not null default '',
age char(20) not null default ''
)engine= InnoDB DEFAULT charset utf8;
The data is arranged in accordance with the column name, and multiple pieces of data can be inserted.
INSERT INTO table_name (field1, field2,...fieldN) VALUES (valueA1,valueA2,...valueAN),(valueB1,valueB2,...valueBN),(valueC1,valueC2,...valueCN)...;

2, idea project establishment project


File in the upper left corner of the new project of idea should follow the figure step by step. Specify jdk, next in the lower right corner. Next, you can directly next, write your package name, Finish.

3, Import the jar package of the linked database

Where is jar from?

Good question. Go to this place https://dev.mysql.com/downloads/connector/j/

Pour the jar package into your project as shown in Figure 1-5. Step 5 is to see if you can import the jar correctly.

So far, the preparatory work is over. You can write your own code to operate the data. As a little vegetable chicken, it reminds the big guy that the database is a database, and the database client is a database client. The database is divided into relational database {MySQL, Oracle, DB2, Microsoft SQL Server} and so on. Non relational database {Redis, MongoDB}, big data and hive, Hbase and so on are also useful to Es as a database. The database clients include sqlyog, Navicat, etc. because the data is made naked, some gentlemen can't stand it, so they make clothes for these excellent databases for free.

4, Enter the formal simple operation stage.

1. First of all, we need to see whether our project can run the program.

2. The user name and pass word are defined by myself. Please specify the user name and password of your database.

Just paste it into the main method.

        Connection connection;
        String driver = "com.mysql.jdbc.Driver";
        //The URL points to the database name mydata to be accessed
        String url = "jdbc:mysql://10.37.125.33:3306/test";
        //User name when MySQL is configured
        String user = USER_NAME;
        //Password for MySQL configuration
        String password = PASS_WORD;

        try {
            // Register JDBC Driver
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);

            if (!connection.isClosed()) {
                System.out.println("Succeeded connecting to the Database!");
                //2. Create statement class object to execute SQL statement!!
                Statement statement = connection.createStatement();
                String sql = "select * from index_code_desc";
                ResultSet rs = statement.executeQuery(sql);
                String indexCode = null;
                String indexDesc = null;
                String function = null;
                String functionWhere = null;
                String functionTimeType = null;
                String functionOrderBy = null;
                String dimension = null;
                String event = null;
                String indexParallelism = null;
                String topologyName = null;
                while(rs.next()){
                    //Get sno data
                    indexDesc = rs.getString("indexDesc");
                    //Get sname
                    indexCode = rs.getString("indexCode");
                    //Get the data of sex
                    function = rs.getString("function");
                    //Get the data of age
                    functionWhere = rs.getString("functionWhere");
                    functionTimeType = rs.getString("functionTimeType");
                    functionOrderBy = rs.getString("functionOrderBy");
                    dimension = rs.getString("dimension");
                    event = rs.getString("event");
                    indexParallelism = rs.getString("indexParallelism");
                    topologyName = rs.getString("topologyName");
                    //Output result
                    System.out.println(indexDesc + "\t" + indexCode + "\t" + function + "\t" + functionWhere
                            + "\t" + functionTimeType
                            + "\t" + functionOrderBy
                            + "\t" + dimension
                            + "\t" + event
                            + "\t" + functionTimeType
                            + "\t" + indexParallelism
                            + "\t" + indexParallelism
                            + "\t" + topologyName
                    );
                }
                rs.close();
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 

This is just the simplest way to connect data without maven project, and the code has not been carefully sorted out and standardized, which is forgiven by powerful people.

Published 1 original article, praised 0, visited 8
Private letter follow

Posted by jimmyo on Mon, 20 Jan 2020 23:18:55 -0800