Java_jdbc 基础笔记之一 数据库连接
方式一:
1、创建一个Driver实现类的对象
2、准备连接数据库的基本信息:url,user,password
3、调用Driver接口的connect(url,info)获取数据库连接
* Driver 是一个接口:数据库厂商必须提供实现的接口,能从其中获取数据库的连接。 1.加入mysql驱动
* 1)解压mysql-connector-java-5.1.7.zip
* 2)在当前项目下新建lib目录
* 4)右键bulid-path,add to
* buildpath 加入到类路径下
@Test
public void testDriver() throws SQLException {
// 1 创建Driver实现类的对象
Driver driver = new Driver();
// 2、准备连接数据库的基本信息:url,user,password
String url = "jdbc:mysql://127.0.0.1:3306/test";
Properties properties = new Properties();
properties.put("user", "root");
properties.put("password", "admin");
// 3、调用Driver接口的connect(url,properties)获取数据库连接
Connection conn = driver.connect(url, properties);
System.out.println(conn);
}
方式二:
/**
* 编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
* 解决方案:把数据库驱动Driver实现类的全类名、url、user、pasword 放入一个配置文件中,
* 通过修改配置文件的方式实现和具体的数据库的解耦
*
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*
*/ public Connection getConnection() throws Exception {
String driverClass = null;
String urljdbc = null;
String user = null;
String password = null;
// 读取类路径下的jdbc.properties文件
InputStream is = getClass().getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(is);// .....加载进来
driverClass = properties.getProperty("driver");
urljdbc = properties.getProperty("urljdbc");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 通过反射创建Driver对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();// 反射!!
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
// 通过Driver的connect方法获取数据库的连接
Connection connection = driver.connect(urljdbc, info);
return connection; } @Test
public void testConnection() throws Exception {
System.out.println(getConnection());
}
转: https://blog.csdn.net/YL1214012127/article/details/48210857
Java_jdbc 基础笔记之一 数据库连接的更多相关文章
- Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)
public Student getStudent(String sql, Object... args) { // 查询Student对象 Student stu = null; Connectio ...
- Java_jdbc 基础笔记之七 数据库连接(方法升级)
之前的更新方法 public static void update(String sql) { Connection conn = null; Statement statement = null; ...
- Java_jdbc 基础笔记之六 数据库连接 (PreparedStatement)
reparedStatement 是 Statement 的子接口 * ①需要预编译 SQL 语句:PreparedStatement ps = conn.preparedStatement(sql) ...
- Java_jdbc 基础笔记之五 数据库连接 (ResultSet)
/** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. * 1. 调用 Statement 对象的 executeQuery(sql)可以得到结果集. * 2. Resul ...
- Java_jdbc 基础笔记之四 数据库连接 (通用更新方法)
/** * 写一个通用的更新方法 包括 INSERT. DELETE.UPDATE * 使用工具类 * @param sql */ public void update(String sql){ Co ...
- Java_jdbc 基础笔记之三 数据库连接 (Statement)
/** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...
- Java_jdbc 基础笔记之二 数据库连接
/** * DriverManager 类是驱动程序管理器类 * 1)可以通过重载的getConnection()方法获取数据库的连接,较为方便 * 2)可以同时管理多个驱动程序:若注册了多个数据库连 ...
- Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)
public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...
- Java_jdbc 基础笔记之十四 数据库连接(元数据)数据库信息及连接信息
public class MetaDatatest { /** * DatabaseMetaData 是描述 数据库的元数据对象 可以由Connection得到 */ @Test public voi ...
随机推荐
- MySQL Backup--Xtrabackup远程备份和限速备份
使用xbstream 备份到远程服务器 ##xbstream 备份到远程服务器 innobackupex \ --defaults-file="/export/servers/mysql/e ...
- VLAN实验3:理解Hybrid接口的应用
实验环境 实验拓扑图 实验编址 实验步骤1.基本配置按照实验编址为PC配置IP地址,以PC5为例 在PC5与PC1通过ping命令测试,发现通讯正常.(以此为例,其他的我就不一一截图测试了.) 在S1 ...
- xtarbackup 简单恢复
xtrbackup Xtrabackup安装 #下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/ep ...
- altium designer的pcb板如何移动到原点?
可以把所有的都选中,然后将光标移到起点处,将所有的移到原点的地方,但这种做法很多时候都不好:比较好的办法就是将原点设置到起点上来. 具体做法是:edit--origin --set. 这时光标成了十字 ...
- Qt错误: 程序数据库管理器不匹配 请检查安装
错误提示: C1902: 程序数据库管理器不匹配:请检查安装解决 解决方法: 到D:\VisualStudio2015\VC\bin目录下面拷贝mspdbsrv.exe.mspdb140.dll.ms ...
- 适合公司和个人的目标管理方法:OKR!
1.定义 OKR就是Objectives and Key Results的简称,包括目标(Objectives)和关键结果(Key Results)两个要素. 2.目的 就公司和团队而言 ...
- Jmeter4.0 _Beanshell解析并获取json响应数据数组长度
我们在做jmeter接口测试的时候,有时候碰到开发没返回数据total,只返回了一条条记录,可是呢,我们又需要知道到底返回了多少条数据时,咋办呢?咋办呢?咋办呢? 不要急,接下来,让我们见证奇迹是如何 ...
- 利用Python openpyxl操作Excel
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = ...
- kafka,activemq rabbitmq.rocketmq的优点和缺点
kafka,activemq rabbitmq.rocketmq的优点和缺点: 特性 ActiveMQ RabbitMQ RocketMQ Kafka 单机吞吐量 万级,吞吐量比RocketMQ和Ka ...
- K-means: optimization objective(最小化cost function来求相应的参数)
类似于linear regression,K-means算法也optimization objective或者是试图使cost function求最小值. 了解K-means算法的optimizati ...