JDBC_获取数据库连接
来源:互联网 发布:淘宝apple store优惠券 编辑:程序博客网 时间:2024/05/18 12:36
<span style="font-size:24px;">package src.com.jdbc.java;import java.io.IOException;import java.io.InputStream;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;import org.junit.Test;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Driver;public class JDBCTest {@Testpublic void testGetConnection2() throws Exception{System.out.println(getConnection2());}public Connection getConnection2() throws Exception{//1、准备连接数据库的四个字符//1)、创建properties对象Properties properties=new Properties();//2)、获取jdbc.properties对象InputStream in=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");//3)、加载2)对应的输入流properties.load(in);//4)、具体决定user,passwrod等四个字符串String user=properties.getProperty("user");String password=properties.getProperty("password");String jdbcUrl=properties.getProperty("jdbcUrl");String driver=properties.getProperty("driver");//2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)Class.forName(driver);//3、通过DriverManager的getConnection()方法获取数据库的连接。return (Connection) DriverManager.getConnection(jdbcUrl,user,password);}/** * DriverManager 是驱动的管理类. * 1、可以通过重载的getconnection()方法获取连接, * 2、可以同时管理多个驱动程序;注册了多个数据库连接,则调用getconnection()方法 * 时传入的参数不同,即返回不同的数据库连接 * @throws IOException * @throws ClassNotFoundException * @throws SQLException * */@Testpublic void testDriverManager() throws Exception{//1、准备连接数据库的四个字符String driverClass = null;String jdbcUrl = null;String user = null;String password = null;// 读取类路径下jdbc.propertiesInputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");Properties properties = new Properties();properties.load(in);driverClass = properties.getProperty("driver");jdbcUrl = properties.getProperty("jdbcUrl");user = properties.getProperty("user");password = properties.getProperty("password");//2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)Class.forName(driverClass);//3、通过DriverManager的getConnection()方法获取数据库的连接。Connection connection=(Connection) DriverManager.getConnection(jdbcUrl,user,password);System.out.println(connection);}@Testpublic void testDriver() throws SQLException {Driver driver = new com.mysql.jdbc.Driver();String url = "jdbc:mysql://127.0.0.1:3306/cjl";Properties info = new Properties();info.put("user", "root");info.put("password", "root123");Connection connection = (Connection) driver.connect(url, info);System.out.println(connection);}/** * 编写一个通用方法:把数据库驱动Driver实现类的全类名、URL、user、password放入 * 一个配置文件中,通过修改配置文件的方式实现和具体的数据库解偶 * * @throws Exception */public Connection getConnection() throws Exception {String driverClass = null;String jdbcUrl = null;String user = null;String password = null;// 读取类路径下jdbc.propertiesInputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");Properties properties = new Properties();properties.load(in);driverClass = properties.getProperty("driver");jdbcUrl = properties.getProperty("jdbcUrl");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的connection方法获取数据库连接Connection connection = (Connection) driver.connect(jdbcUrl, info);return connection;}@Testpublic void testGetConnection() throws Exception{System.out.println(getConnection());}}</span>
jdbc.properties文件中的配置
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/cjl
user=root
password=root123
0 0
- JDBC_获取数据库连接
- JDBC_数据库连接池
- JDBC_数据库连接池(一) 介绍
- JDBC_获取插入记录的主键值
- JDBC_获取插入记录的主键值(基于MySQL数据库)
- JDBC_数据库连接池(二) DNCP与C3P0的使用方法及代码演示
- JDBC_事务
- 获取数据库连接-bboss persistent
- weblogic下获取数据库连接
- 数据库连接池获取连接
- java 获取数据库连接
- jdbc获取数据库连接
- jdbc获取各种数据库连接
- 快速获取数据库连接字串
- 快速获取数据库连接字串
- 简单获取数据库连接串
- 从jndi获取数据库连接
- 巧妙获取数据库连接字符串
- JAVAEE中表单jQuery的检验方法,简单快捷...
- Eclipse将Web项目部署到tomcat
- C语言基础—流程控制
- 链式队
- OpenCVR wiki
- JDBC_获取数据库连接
- 硬盘安装Win7教程!无光驱无U盘照样装Win7
- .Net 后台获取GridView里<TemplateField>的控件值是否变化
- lr11破解
- 华为2014机试字符串压缩
- android webview js交互 第一节 (java和js交互)
- Reverse Linked List 解法与测试
- 在使用requests模块时报错:InsecurePlatformWarning:
- 弹幕效果