Use jdbc, java and database connection to realize the method of accessing data in the database in java code ---- the simplest entry level

Posted by jack_wetson on Fri, 26 Nov 2021 19:22:06 +0100

I am a computer white, just started self-study, slowly improving

It's lucky to knock the code and correct the mistakes. Please leave your praise. It's my greatest encouragement and gives the little girl the confidence to continue to improve! Thank you

jdbc download address Click here to download

For specific download methods, please refer to the operation methods of other bloggers. Their explanations are particularly detailed,

first

First step   Create a java_mysql connection. When creating this connection, you must remember the user and password when you create it. My user is root and the password is 123456,   Create a database data and create a data table named info in the database data. Note that this data table will be accessed by java in the following operations. For simplicity, I use navicat to create it here,

  Then directly create the data table and add content in it. The results are shown in the figure

  We'll finish creating the database first

Next, go to the java section

1. Prepare the driver and jar package. Pay attention to the version of jar package. My mysql version is more than 5. So my driver version is more than 5. If the sql version is more than 8.0, you need to configure more than 10. Jar drivers

 

Note: this driver is necessary,

2. Enter eclipse to prepare java code

First put the prepared driver file into it, right-click the stand-alone on src, create a package named lib, and put the driver into the Lib package

  Open the lib package, place the mouse over the driver file, right-click to select the bulid path option, and continue to select add build path. In this way, your driver can be used normally in java and play the role you want

If you finish the above operation, it will look like this

  3. Prepare the java code. In fact, it is very simple. After all, what I write is also very simple and involves little knowledge. Since it is a beginner, in order to understand it, I write a simple one first and understand it thoroughly before processing

Annotations I explain are simple and easy to understand. After all, blogger himself is a fool. Don't make complaints about me too much! I'll try to improve
 

 

/JDBC Tool class for
//Because the user password in jdbc is fixed, you need to write a variable database. If you want to connect to other databases, you can modify it in this class, which is convenient and fast
//All fields linked to the record database are separated from the source code
//To combine variable quantities with java
//Note: to encapsulate this class as a tool
//The db.properties file is a resource file
package com.tlj.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.mysql.jdbc.Connection;

//Once the class is loaded, the driver is automatically loaded
public class JDBCUtils 
{
    //Static statement block
    static 
    {   //Class method, get an object, getClassLoader() class loader
//         Read the file db.properties into the program in the form of input stream
//         The inputstream object points to a stream file. Because the return value type is a stream, the stream file is finally converted into a data resource db
        InputStream inputstream = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
//         Create an object of type properties
        Properties p=new Properties();
//         Load the stream file. After this step, you can get the value of drivers in the db file
        try 
        {
            p.load(inputstream);
            //Load the input stream object of the file into the properties class
//             p can access the contents of the db file resource file. driver is a string type, so double quotation marks are used
            String driver = p.getProperty("driver");
//             Load the driver. Because the driver is in the db file, you have to find the db file and read the value of db and place it in the driver
            Class.forName(driver);
            System.out.println("Driver loaded successfully");
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            e.getClass();
            e.toString();
            System.out.println("An exception appears here. Please handle it in time");
        }           
    }
}

All right! Here we can realize the most basic access to the database and java. The blogger will update the deeper code soon. The code has been written and will be added in more detailed comments.

 

 

Topics: Linux Operation & Maintenance server