Home >Database >Mysql Tutorial >MySQL learning Java graphic code example to connect to MySQL database

MySQL learning Java graphic code example to connect to MySQL database

黄舟
黄舟Original
2017-09-08 14:24:101735browse

1. What is JDBC?

JDBC (Java DataBase Connectivity) is Java database connection. To put it bluntly, it uses Java language to operate the database. It turns out that when we operate the database, we use SQL statements on the console to operate the database. JDBC uses Java language to send SQL statements to the database.

2. JDBC principle

SUN provides access to the database specification called JDBC, and the implementation class provided by the manufacturer is called a driver.

JDBC is the interface, and the JDBC driver is the implementation of the interface. The database connection cannot be completed without the driver!

Each database manufacturer has its own driver for connecting to its own company's database.

3. JDBC development steps

1) Register driver

2) Obtain connection

3) Obtain statement executor

4 ) Execute sql statement

5) Process the result

6) Release resources

3. Import the driver jar package

1) Create a new project and name it WEB08_JDBC ,

2) Create the lib directory, right-click New->Folder, name it lib, which is used to store all jar packages required by the current project,

Copy the jar package to Under the lib folder of the current project,

3) Select the jar package, right-click and execute Build Path until a small milk bottle logo appears in the current directory

4 , Test the sql injection problem (apply to the content of JUnit unit test)

There are two pieces of data in the tbl_user table in the web08 database under mysql, and log in according to the user information.

The specific code implementation is as follows:

package cn.itheima.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class TestLogin {
        @Test
        public void testLogin(){
            try {
                login1("zhangsan","999");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void login1(String username,String password) throws ClassNotFoundException, SQLException{
            /*
             * 用户登录方法
             */
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/web08","root","12345");
            //3.编写sql语句
            String sql="select * from tbl_user where uname=? and upassword=?";
            //4.创建预处理对象
            PreparedStatement pstmt=conn.prepareStatement(sql);
            //5.设置参数(给占位符)
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            //6.执行查询操作
            ResultSet rs=pstmt.executeQuery();
            //7.对结果集进行处理
            if(rs.next()){
                System.out.println("恭喜您,"+username+"登录成功!");
            }else{
                System.out.println("账号或密码错误!");
            }
            if(rs!=null) rs.close();
            if(pstmt!=null) pstmt.close();
            if(conn!=null) conn.close();            
        }
    }

Right-click Run As->JUnit Test, the execution result is: Congratulations, zhangsan logged in successfully!

The above is the detailed content of MySQL learning Java graphic code example to connect to MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn