JDBC的五种连接方式
生活随笔
收集整理的這篇文章主要介紹了
JDBC的五种连接方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC的五種連接方式
1.直接實例化Driver
Driver driver=new com.mysql.jdbc.Driver(); String url="jdbc:myslq://local:3306/database"; Properties info=new Properties(); info.setProperty("user","root"); info.setProperty("password","****"); Connection con=driver.connect(url,info);2.反射實現Driver類
//更具有通用性 Class clazz=Class.forName("com.mysql.jdbc.Driver"); Driver driver=(Driver)clazz.newInstance(); String url="jdbc:myslq://local:3306/database"; Properties info=new Properties(); info.setProperty("user","root"); info.setProperty("password","****"); Connection con=driver.connect(url,info);3.使用DriverManager替換Driver接口
Class clazz=Class.forName("com.mysql.jdbc.Driver"); Driver driver=(Driver)clazz.newInstance(); //注冊驅動 DriverManager.registerDriver(driver); String url="jdbc:myslq://local:3306/database"; String user="root"; String password="****"; Connection con=DriverManager.getConnection(url,user,password);4.利用mysql的driver實現類自動進行了注冊驅動,直接調用DriverManager的靜態方法連接
//省略注冊驅動 //在MySQL的driver實現類的靜態代碼塊中已進行了方法三的操作 Class.forName("com.mysql.jdbc.Driver");//mysql這句也可以省 String url="jdbc:myslq://local:3306/database"; String user="root"; String password="****"; Connection con=DriverManager.getConnection(url,user,password);5.將連接的四個基本信息聲明在配置文件中,讀取配置文件進行連接
jdbc.properties
總結
以上是生活随笔為你收集整理的JDBC的五种连接方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CVE-2018-7490 uWSGI
- 下一篇: Gmail邮箱登陆问题解决方案