1データベースとの確立

1227 ワード

import java.sql.*;

/**
 * 1:           
 *  MySQL  
 */
public class TestConnection {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            //1:     
            Class.forName("com.mysql.jdbc.Driver");
            
            //2:    (          Socket  ,        ,    ,  Connection         )
            //      ,            
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test","root","123456");
                                
            //localhost      : 127.0.0.1
            //test      
            //root MySQL  
            //123456 MySQL  
            
            //com.mysql.jdbc.JDBC4Connection@44bd928a
            System.out.println(conn);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}