当前位置:网站首页>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); } }
收藏起来 忘了可以看看 !!
别忘了点赞哦
边栏推荐
- 使用 flask_whooshalchemyplus jieba实现flask的全局搜索
- Markdown font color editing teaching
- Solutions to common problems in database development such as MySQL
- SQL injection
- Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
- 链队实现(C语言)
- Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
- 《统计学》第八版贾俊平第二章课后习题及答案总结
- 关于交换a和b的值的四种方法
- flask实现强制登陆
猜你喜欢
Internet Management (Information Collection)
《统计学》第八版贾俊平第四章总结及课后习题答案
Solutions to common problems in database development such as MySQL
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
Hcip -- MPLS experiment
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
关于交换a和b的值的四种方法
Intranet information collection of Intranet penetration (4)
Intranet information collection of Intranet penetration (3)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
随机推荐
DVWA (5th week)
JDBC事务、批处理以及连接池(超详细)
Only 40% of the articles are original? Here comes the modification method
What language should I learn from zero foundation. Suggestions
Low income from doing we media? 90% of people make mistakes in these three points
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
函数:字符串反序存放
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
《统计学》第八版贾俊平第一章课后习题及答案总结
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Pointeurs: maximum, minimum et moyenne
How to test whether an object is a proxy- How to test if an object is a Proxy?
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
Build domain environment (win)
数据库多表链接的查询方式
Hackmyvm target series (1) -webmaster
Hcip -- MPLS experiment
Intranet information collection of Intranet penetration (3)
Xray and Burp linked Mining
【指针】删除字符串s中的所有空格