方式一: 
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 基础笔记之一 数据库连接的更多相关文章

  1. Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)

    public Student getStudent(String sql, Object... args) { // 查询Student对象 Student stu = null; Connectio ...

  2. Java_jdbc 基础笔记之七 数据库连接(方法升级)

    之前的更新方法 public static void update(String sql) { Connection conn = null; Statement statement = null; ...

  3. Java_jdbc 基础笔记之六 数据库连接 (PreparedStatement)

    reparedStatement 是 Statement 的子接口 * ①需要预编译 SQL 语句:PreparedStatement ps = conn.preparedStatement(sql) ...

  4. Java_jdbc 基础笔记之五 数据库连接 (ResultSet)

    /** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. * 1. 调用 Statement 对象的 executeQuery(sql)可以得到结果集. * 2. Resul ...

  5. Java_jdbc 基础笔记之四 数据库连接 (通用更新方法)

    /** * 写一个通用的更新方法 包括 INSERT. DELETE.UPDATE * 使用工具类 * @param sql */ public void update(String sql){ Co ...

  6. Java_jdbc 基础笔记之三 数据库连接 (Statement)

    /** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...

  7. Java_jdbc 基础笔记之二 数据库连接

    /** * DriverManager 类是驱动程序管理器类 * 1)可以通过重载的getConnection()方法获取数据库的连接,较为方便 * 2)可以同时管理多个驱动程序:若注册了多个数据库连 ...

  8. Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)

    public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...

  9. Java_jdbc 基础笔记之十四 数据库连接(元数据)数据库信息及连接信息

    public class MetaDatatest { /** * DatabaseMetaData 是描述 数据库的元数据对象 可以由Connection得到 */ @Test public voi ...

随机推荐

  1. javascript_15-undefined 和 is not defined 的区别

    undefined 和 is not defined //1 console.log(a); // is not defined //2 var a; console.log(a); //undefi ...

  2. 二叉搜索树(python)

    # -*- coding: utf-8 -*- class BSTNode(object): def __init__(self, key, value, left=None, right=None) ...

  3. # Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体

    在controller中取出emps 对象数组 //1.查询所有的员工,返回列表页面 @GetMapping("/emps") public String list(Model m ...

  4. Redis 缓存雪崩、穿透、击穿

    缓存雪崩 定义: 同一时间所有 key 大面积失效,比如网站首页的数据基本上都是同一批次去缓存的. 解决方法: ① 存的时候设定随机的失效时间. ② 服务做熔断处理(异常或着慢查询 Hystrix 限 ...

  5. Spring源码窥探之:Condition

    采用注解的方式来注入bean 1. 编写config类 /** * @author 70KG * @Title: ConditionConfig * @Description: * @date 201 ...

  6. 针对jar里面的图片不显示问题

    做了一个html生产pdf案例. 然后把图片放到resource/static/model/img下面,生成jar包运行,发现图片不显示, 发现html里面的src必须是http域名开头的图片. 下面 ...

  7. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors

    链接: https://codeforces.com/contest/1265/problem/E 题意: Creatnx has n mirrors, numbered from 1 to n. E ...

  8. 5、Hadoop 2.6.5 环境搭建

    下载 地址:http://archive.apache.org/dist/hadoop/common/ sudo wget http://archive.apache.org/dist/hadoop/ ...

  9. [TypeScript] Modifier

    TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. Spe ...

  10. 洛谷 P3375 【模板】KMP字符串匹配 题解

    KMP模板,就不解释了 #include<iostream> #include<cstdio> #include<cstring> #include<algo ...