자바 my sql 데이터 베 이 스 를 연결 하 는 세 가지 방법

1578 단어
package day17;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;


public class TestConnection {

	/**
	 * @param args
	 * @throws SQLException 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws SQLException, Exception {
			
		/*
		 *             
		 */
		String url="jdbc:mysql://localhost:3306/test";
		//    
		/*
		 * Class.forName(String classPath)
		 *        ,           ,           
		 *   com.mysql.jdbc.Driver        ,    
		 */
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn=(Connection) DriverManager.getConnection(url, "root", "");
		System.out.println("conn= "+conn);
			
	}
	
	/*
	 *          
	 */
	public static void registDriver() throws Exception{
		String url="jdbc:mysql://localhost:3306/test";
		//    
		DriverManager.registerDriver(new Driver());
		//    
		Connection conn=(Connection) DriverManager.getConnection(url, "root", "");
		System.out.println("conn= "+conn);
	}
	
     /*
      *   Driver    
      */
	public static void testDriver() throws Exception{
		Driver driver=new Driver();
		Properties info=new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "");
		String url="jdbc:mysql://localhost:3306/test";
		
		Connection conn=(Connection) driver.connect(url, info);
		System.out.println("conn= "+conn);
	}
}

좋은 웹페이지 즐겨찾기