记得下载jdbc的插件jar,然后添加库
import com.mysql.cj.jdbc.Driver; import com.sun.xml.internal.ws.developer.UsesJAXBContext; import org.junit.Test; 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.sql.Statement; import java.util.Properties; //连接mysql的6种方法
@Test @SuppressWarnings({"all"}) public void show1() throws SQLException { //灵活性低,依赖性高 //前置工作,在项目下创建目录,把mysql.jar 拷贝改目录下,点击add to project (添加为库) //1.注册驱动 Driver driver=new com.mysql.jdbc.Driver(); //创建Deiver对象 加载驱动 //2.得到连接 String url="jdbc:mysql://localhost:3306/db01"; //将用户名和密码放入到Properties对象 Properties properties=new Properties(); properties.setProperty("user","root"); properties.setProperty("password","sasa"); Connection connection= driver.connect(url,properties); //连接mysql }
@Test public void show2() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { //使用反射加载Driver类 动态加载,更加的灵活,减少依赖性 Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) aClass.newInstance(); String url="jdbc:mysql://localhost:3306/db01"; Properties properties=new Properties(); properties.setProperty("user","root"); properties.setProperty("passwrd","sasa"); Connection connect= driver.connect(url,properties); System.out.println("方法2"+connect); } //方法3 使用DriverManager 替代 Properties @Test public void show3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { final Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver=(Driver) aClass.newInstance(); String url="jdbc:mysql://localhost:3306/db01"; DriverManager.registerDriver(driver);//注册Driver驱动z Connection connection=DriverManager.getConnection(url,"root","sasa"); } //方法4 使用Class.forName 自动完成驱动,简化代码 @Test public void show4() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); //底层逻辑 静态块会自动注册Driver 驱动com.mysql.cj.jdbc.Driver 源代码已经自动加载驱动程序类 Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db01","root","sasa"); System.out.println("第4种方法:"+connection); connection.close(); } @Test @SuppressWarnings({"all"}) //对被批注的代码元素内部的某些警告保持静默。 public void show5() throws SQLException { //mysql驱动5.1.6就可以无需Class.forName() //从jdk1.5以后使用jdbc4,不在需要调用class.forName()注册驱动而是自动调用驱动 // jar包下META-INF包下面的services的java.sql.Driver有 Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db01","root","sasa"); System.out.println("第5种方法"+connection); connection.close(); } @Test public void show6() throws IOException, ClassNotFoundException, SQLException { //添加配置文件,让mysql更加灵活 //通过Properties对象获取文件的信息 Properties properties=new Properties(); properties.load(new FileInputStream("src\\mysql.properites")); String user=properties.getProperty("user"); //通过key值获取Value值 String password=properties.getProperty("password"); String driver= properties.getProperty("driver"); String url= properties.getProperty("url"); Class.forName(driver); Connection connection=DriverManager.getConnection(url,user,password); System.out.println("配置文件"+connection); }
}
配置文件,在src右键新建File名字为mysql.properites
根据自己的名字和密码进行修改
user=root
password=sasa
url=jdbc:mysql://localhost:3306/db01
driver=com.mysql.cj.jdbc.Driver