当前位置:网站首页>The four connection methods of JDBC are directly coded

The four connection methods of JDBC are directly coded

2022-07-06 14:41:00 MSjan

Don't talk nonsense Code up Original handwriting

package JDBCbutton.FristStudy;

import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Day02 {
    public void connect1() throws SQLException {
        //1. Registration drive 
        Driver driver = new Driver();
        //2. Get links 
        //jdbc:mysql://  Is agreement 
        //localhost  host , It can be IP Address 
        //3306  Express mysql Listening port 
        //hsp_db02  Connected database 
        String url = "jdbc:mysql://localhost:3306/zzs_db01";
        //  hold   Account and password   Put in Properties  In the object 
        Properties properties =new Properties();
        //user  and  password  It's prescribed   The actual situation later is 
        properties.setProperty("user","root");
        properties.setProperty("password","123123");

        // Get the connection 
        Connection connect  =driver.connect(url,properties);
    }
    @Test
    public void connect2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
       Class<?> aClass =Class.forName("com.mysql.jdbc.Driver");
       //
        Driver driver =(Driver) aClass.newInstance();

        String url = "jdbc:mysql://localhost:3306/zzs_db01";

        Properties properties =new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123123");

        Connection connect  =driver.connect( url ,properties);

        System.out.println(" Mode two = " +connect);
    }
    @Test
  

    public void connect3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass =Class.forName("com.mysql.jdbc.Driver");

        Driver driver =(Driver) aClass.newInstance();
        String url ="jdbc:mysql://localhost:3306/zzs_db01";
        String user="root";
        String password="123123";

        DriverManager.registerDriver(driver);

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

        System.out.println(" Method 3  = "+connection);


    }
    @Test
    public void connect4() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");// Use reflection to load 


        String url ="jdbc:mysql://localhost:3306/zzs_db01";
        String user="root";
        String password="123123";

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

        System.out.println(" The fourth method ="+connection);
    }

}

Collect it If you forget, you can have a look !!

Don't forget to like it

原网站

版权声明
本文为[MSjan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919019474.html