当前位置:网站首页>JDBC 的四种连接方式 直接上代码
JDBC 的四种连接方式 直接上代码
2022-07-06 09:24:00 【MSjan】
废话少说 上代码 原创手写
package JDBCbutton.FristStudy; import com.mysql.jdbc.Driver; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class Day02 { public void connect1() throws SQLException { //1.注册驱动 Driver driver = new Driver(); //2.得到链接 //jdbc:mysql:// 是协议 //localhost 主机,可以是IP地址 //3306 表示mysql监听的端口 //hsp_db02 连接的数据库 String url = "jdbc:mysql://localhost:3306/zzs_db01"; // 把 账号密码 放入Properties 对象中 Properties properties =new Properties(); //user 和 password 是规定好的 后面实际情况写 properties.setProperty("user","root"); properties.setProperty("password","123123"); //获取连接 Connection connect =driver.connect(url,properties); } @Test public void connect2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Class<?> aClass =Class.forName("com.mysql.jdbc.Driver"); // Driver driver =(Driver) aClass.newInstance(); String url = "jdbc:mysql://localhost:3306/zzs_db01"; Properties properties =new Properties(); properties.setProperty("user","root"); properties.setProperty("password","123123"); Connection connect =driver.connect( url ,properties); System.out.println("方式二= " +connect); } @Test public void connect3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Class<?> aClass =Class.forName("com.mysql.jdbc.Driver"); Driver driver =(Driver) aClass.newInstance(); String url ="jdbc:mysql://localhost:3306/zzs_db01"; String user="root"; String password="123123"; DriverManager.registerDriver(driver); Connection connection = DriverManager.getConnection(url, user, password); System.out.println("方法三 = "+connection); } @Test public void connect4() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver");//使用反射加载 String url ="jdbc:mysql://localhost:3306/zzs_db01"; String user="root"; String password="123123"; Connection connection = DriverManager.getConnection(url, user, password); System.out.println("第四种方法="+connection); } }
收藏起来 忘了可以看看 !!
别忘了点赞哦
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- 攻防世界MISC练习区(gif 掀桌子 ext3 )
- Markdown font color editing teaching
- 函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
- c语言学习总结(上)(更新中)
- {1,2,3,2,5}查重问题
- The most popular colloquial system explains the base of numbers
- Intranet information collection of Intranet penetration (I)
- 指针--剔除字符串中的所有数字
- XSS unexpected event
猜你喜欢
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
Interview Essentials: what is the mysterious framework asking?
Intel oneapi - opening a new era of heterogeneity
Record once, modify password logic vulnerability actual combat
《英特尔 oneAPI—打开异构新纪元》
图书管理系统
Internet Management (Information Collection)
攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
数字电路基础(一)数制与码制
随机推荐
Circular queue (C language)
Record once, modify password logic vulnerability actual combat
Wu Enda's latest interview! Data centric reasons
《统计学》第八版贾俊平第三章课后习题及答案总结
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
2022华中杯数学建模思路
MySQL learning notes (stage 1)
Based on authorized access, cross host, and permission allocation under sqlserver
Sword finger offer 23 - print binary tree from top to bottom
指针:最大值、最小值和平均值
数字电路基础(四) 数据分配器、数据选择器和数值比较器
函数:求两个正数的最大公约数和最小公倍
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
线程的实现方式总结
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
SystemVerilog discusses loop loop structure and built-in loop variable I
指针 --按字符串相反次序输出其中的所有字符
函数:用牛顿迭代法求方程的根
What language should I learn from zero foundation. Suggestions