博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC Driver接口连接数据库,实际开发基本不用
阅读量:6161 次
发布时间:2019-06-21

本文共 1179 字,大约阅读时间需要 3 分钟。

import java.sql.Connection;
import java.util.Properties;
import com.mysql.jdbc.Driver;
public class JDBCDriver_getConnection {
//通过Driver接口获取连接,实际开发基本不用
public static void main(String[] args) throws Exception {
new JDBCDriver_getConnection().getConnection(); 
}
public Connection getConnection() throws Exception{
//连接参数
String driverClass = null;
String url=null;
String user = null;
String password = null;
Properties properties = new Properties();//使用Properties
properties.load(getClass().getClassLoader().getResourceAsStream("jdbc.properties"));//读取配置文本
//参数变量赋值
user = properties.getProperty("user");
password = properties.getProperty("password");
url = properties.getProperty("url");
driverClass = properties.getProperty("driverClass");
//配置connect()的 Properties
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Driver driver = (Driver) Class.forName(driverClass).newInstance();//注入驱动
Connection connection = (Connection) driver.connect(url, info);//获取连接
return connection;
/*
* 以下是jdbc.properties  配置文件内容,配置文件需放在src根目录下
* driverClass = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3307/Test
user = root
password = 3306
*/
}
}

转载地址:http://hfrfa.baihongyu.com/

你可能感兴趣的文章
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
图解SSH原理及两种登录方法
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JS图片跟着鼠标跑效果
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
416. Partition Equal Subset Sum
查看>>
app内部H5测试点总结
查看>>
[TC13761]Mutalisk
查看>>
while()
查看>>
常用限制input的方法
查看>>