创建JDBC连接的三种方式


package com.atguigu.demo1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

public class TestConnection2 {
	/*
	 *getConnection(url):连接url?user=用户名&password=密码 
	 */
   @Test
	public void testCon1() throws ClassNotFoundException, SQLException{
	   Class.forName("com.mysql.jdbc.Driver");
	   Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/girls?user=root&password=root");
	   System.out.print("成功1");
    }
   @Test
   public void testCon2() throws SQLException, ClassNotFoundException
   {
	  Class.forName("com.mysql.jdbc.Driver");
	  Connection connection=DriverManager.getConnection("jdbc:mysql:///girls","root","root");
	  System.out.println("成功2");
   }
   
   @Test
   public void testCon3() throws SQLException, ClassNotFoundException, FileNotFoundException, IOException
   {
	  Class.forName("com.mysql.jdbc.Driver");
	  Properties properties=new Properties();
	  properties.load(new FileInputStream("src/db.properties"));
	  Connection connection=DriverManager.getConnection("jdbc:mysql:///girls",properties);
	  
	  System.out.println("成功3");
   }
   
   
   
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM