자바 는 JDBC 로 데이터 베 이 스 를 연결 하 는 다섯 가지 방식(IDEA 버 전)을 사용 합 니 다.

JDBC 는 자바 가 데이터 베 이 스 를 방문 하 는 기초 이 며,나머지 my batis 와 JDO,Hibernate 는 모두 jdbc 를 패키지 하기 때문에 JDBC 가 데이터 베 이 스 를 연결 하 는 원 리 를 이해 하 는 것 이 매우 중요 합 니 다!!

준비 작업
1.mysql 의 jar 패 키 지 를 lib 디 렉 터 리 로 가 져 오기

2.가 져 온 jar 패 키 지 를 항목 에 추가
 jar 가방 클릭   선택 하 다.

3.TestConnection 클래스 만 들 기 
   다섯 가지 방식 은 다음 과 같다.

 
/**
 * @author
 * @date 2019
 **/
import org.junit.Test;
 
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
 
/**
 * JDBC  
 */
public class ConnectionTest {
    //   
    @Test
    public void testConnection1() throws SQLException {
        //  driver       
        Driver driver=new com.mysql.jdbc.Driver();
        //url;http://localhost:8080/gmall/hello.jpg
        String url="jdbc:mysql://localhost:3306/student";
        //              Properties 
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
    //    info.setProperty("user","root");
      //  info.setProperty("password","root");
 
 
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
 
    }
 
    //              ;       api             
    @Test
    public void testConnection2() throws Exception{
        //  driver          
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver=(Driver) clazz.newInstance();
 
        //2.         
        String url="jdbc:mysql://localhost:3306/student";
        //3.      
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        //4.    
        Connection connect = driver.connect(url, info);
        System.out.println(connect);
    }
 
    //       drivermanager     driver
    @Test
    public void testConneciont3() throws  Exception{
        //1.  Driver    
        Class clazz=Class.forName("com.mysql.jdbc.Driver");
       Driver driver=(Driver) clazz.newInstance();
 
       //2.             
        String url="jdbc:mysql://localhost:3306/student";
        String user="root";
        String password="root";
 
        //    
        DriverManager.registerDriver(driver);
        //    
        Connection conn=DriverManager.getConnection(url,user,password);
        System.out.println(conn);
 
    }
    //   
    @Test
    public void testConneciont4() throws  Exception{
        //1           
        String url="jdbc:mysql://localhost:3306/student";
        String user="root";
        String password="root";
 
        //2.  Driver         
       Class.forName("com.mysql.jdbc.Driver");
        //      ,      , Driver         
      //  Driver driver=(Driver) clazz.newInstance();
        //    
       // DriverManager.registerDriver(driver);
 
        //3.    
        Connection conn=DriverManager.getConnection(url,user,password);
        System.out.println(conn);
 
    }
    //     (final)                              ,    
 
    /**
     *    
     * 1.           ,     
     * 2,            ,          
     * @throws Exception
     */
    @Test
    public void TestConnection5() throws  Exception{
        //          
        InputStream is=ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros=new Properties();
        pros.load(is);
        String user=pros.getProperty("user");
        String password=pros.getProperty("password");
        String url=pros.getProperty("url");
        String driverClass=pros.getProperty("driverClass");
 
        //2.    
        Class.forName(driverClass);
        //3.    
        Connection conn=DriverManager.getConnection(url,user,password);
        System.out.println(conn);
 
    }
}
다섯 번 째 방법
src 디 렉 터 리 아래 jdbc.properties 파일 을 만 드 는 내용 은 다음 과 같 습 니 다.

그리고 모든 방식 의 실행 결 과 는 연결 성공 을 설명 하 는 것 입 니 다!!!

자바 가 JDBC 를 사용 하여 데이터 베 이 스 를 연결 하 는 다섯 가지 방식(IDEA 버 전)에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 JDBC 연결 데이터 베 이 스 는 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기