日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDBC的五种连接方式

發(fā)布時(shí)間:2023/12/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDBC的五种连接方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

JDBC的五種連接方式

1.直接實(shí)例化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.反射實(shí)現(xiàn)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(); //注冊驅(qū)動(dòng) 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實(shí)現(xiàn)類自動(dòng)進(jìn)行了注冊驅(qū)動(dòng),直接調(diào)用DriverManager的靜態(tài)方法連接

//省略注冊驅(qū)動(dòng) //在MySQL的driver實(shí)現(xiàn)類的靜態(tài)代碼塊中已進(jìn)行了方法三的操作 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.將連接的四個(gè)基本信息聲明在配置文件中,讀取配置文件進(jìn)行連接
jdbc.properties

driverClass=com.jdbc.mysql.Driver user=root password=**** url=jdbc:mysql://localhost:3306/database //數(shù)據(jù)和代碼分離,如有修改只需修改配置文件 InputStream is =JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties"); Properties prop=new Properties(); prop.load(is);String driverClass= prop.getProperty("driverClass"); String url=prop.getProperty("url"); String user=prop.getProperty("username"); String password= prop.getProperty("password"); Class.forName(driverClass); Connection con=DriverManager.getConnection(url,user,password);

總結(jié)

以上是生活随笔為你收集整理的JDBC的五种连接方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。