Review of JDBC programming

Time:2020-8-1

1. Prepare the data

--Create database
CREATE DATABASE mybatis;

--Using databases
USE mybatis;

--Delete table
DROP TABLE IF EXISTS user;

--Create table
CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `Username ` varchar (32) not null comment 'user name',
  `Birthday ` datetime default null comment 'birthday',
  `Sex ` char (1) default null comment 'gender',
  `Address ` varchar (256) default null comment 'address',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--Insert data
Insert into ` user '(` ID', ` username ', ` birthday', ` sex ', ` address') values (41,' Zhang San ','2019-03-27 18:47:08','male ',' Beijing ');
Insert into ` user '(` ID', ` username ', ` birthday', ` sex ', ` address') values (42,' Li Si ','2019-03-27 18:47:08','male ',' Shanghai ');
Insert into ` user '(` ID', ` username ', ` birthday', ` sex ', ` address') values (43,' Wang Wu ','2019-03-27 18:47:08','male ',' Guangzhou ');

2. Building Maven project

File -> New -> Module
Review of JDBC programming

Maven -> Module SDK -> Next
Review of JDBC programming

Finish
Review of JDBC programming

Import coordinates
Review of JDBC programming

3. JDBC programming

import java.sql.*;

public class TestJdbc {
    public static void main(String args[]) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1. Load driver
            Class.forName("com.mysql.jdbc.Driver");
            //2. Get the connection
            String url = "jdbc:mysql://localhost:3306/mybatis";
            String username = "root";
            String password = "root";
            connection = DriverManager.getConnection(url, username, password);
            //3. Get preprocessing object
            String sql = "SELECT * FROM user WHERE id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //Setting parameters
            preparedStatement.setInt(1,41);
            //Execute query
            resultSet = preparedStatement.executeQuery();
            //Processing result set
            while (resultSet.next()) {
                System.out.println(resultSet.getString("username"));
                System.out.println(resultSet.getDate("birthday"));
                System.out.println(resultSet.getString("sex"));
                System.out.println(resultSet.getString("address"));
            }
        }catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            try {
                //Close the connection and free up resources
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}

4. Analysis of JDBC becoming a disadvantage

Review of JDBC programming

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]