当前位置:网站首页>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);
}
}收藏起来 忘了可以看看 !!
别忘了点赞哦
边栏推荐
- On the idea of vulnerability discovery
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- JDBC事务、批处理以及连接池(超详细)
- Constants, variables, and operators of SystemVerilog usage
- 2022华中杯数学建模思路
- 《统计学》第八版贾俊平第五章概率与概率分布
- JDBC看这篇就够了
- Markdown font color editing teaching
- 图书管理系统
猜你喜欢
![Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]](/img/0b/ddbee0b8a34627e13bff5598bbaed8.jpg)
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]

攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)

Fundamentals of digital circuit (V) arithmetic operation circuit

Record an API interface SQL injection practice

《統計學》第八版賈俊平第七章知識點總結及課後習題答案

Only 40% of the articles are original? Here comes the modification method

JVM memory model concept

Web vulnerability - File Inclusion Vulnerability of file operation

Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class

DVWA (5th week)
随机推荐
New version of postman flows [introductory teaching chapter 01 send request]
安全面试之XSS(跨站脚本攻击)
链队实现(C语言)
Intranet information collection of Intranet penetration (I)
Get started with Matplotlib drawing
Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
Intranet information collection of Intranet penetration (2)
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
指针:最大值、最小值和平均值
On the idea of vulnerability discovery
关于超星脚本出现乱码问题
Pointers: maximum, minimum, and average
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Internet Management (Information Collection)
网络基础详解
Sentinel overall workflow
DVWA (5th week)
Pointeurs: maximum, minimum et moyenne
《统计学》第八版贾俊平第二章课后习题及答案总结
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution