My first JDCB program

1, Create your own database

Download and install MySQL Download:

2. Open the server: enter the bin directory of the download path in the terminal (cmd), and execute mysqld -- initialize executor to start the server and initialize, but an error will be reported. The reason is that there is no data folder in your directory. At this time, you need to create a new data folder, and then execute it again, and it will succeed. You will find that there are many files in your data folder ;

3. Client connection: enter the command through the terminal: MySQL - U root - P, that is, log on to the root user (need to enter the password), the root user is empty by default, and you can enter the sql statement after connecting;

4. To avoid having to enter the bin directory every time you start the service, you can add the bin directory to the environment variable path, so that you can input mysqld to start the server in any directory terminal; 5. You can also make mysql server into a Windows service by inputting mysqld --install in the bin directory; net start mysql and other commands are to start and stop mysql service in the already made windows service in the terminal; you can also open and close the server in Task Manager -- service;

Create your own database

After connecting to mysql server, operate in terminal cmd:

create database mydb;
    -- Created a name for mydb Database of;
show databases;
    -- Check all your databases;

Create table

In order to make our database have some data, let's create a student table

create table student(
    nid int not null auto_increment PRIMARY key,
    name varchar(10),
    age int DEFAULT 18
)engine = innodb DEFAULT charset = utf8
    -- Created a student surface

More database statement operations

View blog:

2, Download driver package

Download path: Note: the version downloaded here should be the same as the version of mysql we installed. Check your mysql statement:

select version() from dual;

My version is 8.0.17, so I downloaded mysql-connector-java-8.0.17

3, Let our MyEclipse see our database

1. Open our MyEclipse -- "Window --" show View -- "Other --" DB Browser -- "double click it, and the following screen will appear:

2. Then a new database

3. After new, such a screen will appear and operate according to the prompts in the figure

Connection URL Code inside:

4. If you press Test Driver to test successfully, you can press Finish. Note that you need to enter your database password during the test;
After the test is successful, you will see:

5. When you click Finish, you will see your new database. Double click your database, you will be linked to the database, and then you will see the tables in your database

4, Create your own first JDBC

Let's finally get to the point, come on!!

1. First, you need to create a new Web Project in MyEclipse

2. Then perform the following operations:

3. After execution, you can enter the following code in the java file you just created:

package myFirstJdcb;
import java.sql.*;
public class main {
  public static void main(String[] args) throws Exception {
    //Load driver

    //Set database information
    String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
    String username = "root";
    String password = "Your database password";

    //Connect to database

    Connection connection = DriverManager.getConnection(url, username, password);

    //Execute database object

    Statement statement = connection.createStatement();

    //Writing sql statements

    String sql = "select * from student";

    ResultSet resultSet = statement.executeQuery(sql);

    while ({

    //Release resources


//Code source:

4. Done!!

Keywords: Java Database MySQL SQL

Added by _giles_ on Mon, 11 May 2020 07:51:35 +0300