当前位置:网站首页>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); } }
收藏起来 忘了可以看看 !!
别忘了点赞哦
边栏推荐
- Lintcode logo queries the two nearest saplings
- JDBC事务、批处理以及连接池(超详细)
- 【指针】求解最后留下的人
- MSF generate payload Encyclopedia
- Xray and Burp linked Mining
- List and data frame of R language experiment III
- 指針:最大值、最小值和平均值
- How does SQLite count the data that meets another condition under the data that has been classified once
- “Hello IC World”
- 《统计学》第八版贾俊平第四章总结及课后习题答案
猜你喜欢
《统计学》第八版贾俊平第三章课后习题及答案总结
线程的实现方式总结
图书管理系统
内网渗透之内网信息收集(一)
On the idea of vulnerability discovery
Xray and Burp linked Mining
链队实现(C语言)
Intranet information collection of Intranet penetration (3)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
How does SQLite count the data that meets another condition under the data that has been classified once
随机推荐
Record once, modify password logic vulnerability actual combat
. Net6: develop modern 3D industrial software based on WPF (2)
Feature extraction and detection 14 plane object recognition
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Attack and defense world misc practice area (GIF lift table ext3)
SQL注入
函数:字符串反序存放
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
《统计学》第八版贾俊平第四章总结及课后习题答案
内网渗透之内网信息收集(四)
This article explains in detail how mockmvc is used in practical work
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Network technology related topics
【指针】删除字符串s中的所有空格
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
Xray and Burp linked Mining
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
Based on authorized access, cross host, and permission allocation under sqlserver
JDBC事务、批处理以及连接池(超详细)