1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JDBC中获取连接的几种方式 快来看看吧

JDBC中获取连接的几种方式 快来看看吧

时间:2020-01-11 14:33:05

相关推荐

JDBC中获取连接的几种方式 快来看看吧

注:下面的连接均是以mysql为例,@Test是Junit4的注解用于测试,Properties不会的话可自行百度

可能存在某些不规范的说法或者错误,恳请各位指出错误

方式一:

import com.mysql.cj.jdbc.Driver;import java.sql.Connection;@Testpublic void test1() throws SQLException {//方式1:Driver是mysql包里面的Driver driver=new Driver();//指定urlString url="jdbc:mysql://localhost:3306/jdbc";//将用户名和密码封装到PropertiesProperties properties=new Properties();properties.setProperty("user","root");properties.setProperty("password","1234");//传入url和propertiesConnection connect = driver.connect(url, properties);System.out.println(connect);}

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式二:

import com.mysql.cj.jdbc.Driver;import java.sql.Connection;@Testpublic void test2() throws Exception {//方式二//包名注意是com.mysql.cj.jdbc.Driver还是com.mysql.jdbc.Driver,jar包版本不同包名不同Class clas = Class.forName("com.mysql.cj.jdbc.Driver");//调用newInstance()方法创建一个Driver对象Driver driver = (Driver) clas.newInstance();//指定url,下面方法同方式一String url="jdbc:mysql://localhost:3306/jdbc";//将用户名和密码封装到PropertiesProperties properties=new Properties();properties.setProperty("user","root");properties.setProperty("password","1234");Connection connect = driver.connect(url, properties);System.out.println(connect);}

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式三:通过反射以及调用DriverManager的getConnection方法获取连接

import java.sql.DriverManager;import java.sql.Connection;@Testpublic void test3() throws Exception {//方式三//通过反射Class.forName("com.mysql.cj.jdbc.Driver");//指定url,数据库的用户名和密码String url="jdbc:mysql://localhost:3306/jdbc";String user="root";String password="1234";//调用DriverManager的getConnection方法获取连接,在idea中按住ctrl+p可以看参数,把url,用户名,密码分别填上。Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);}

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式四:最终版,读取properties文件

首先,在类加载目录下创建一个properties文件,例如mysql.properties;maven工程的话可以在resource目录下建

user=root //数据库的用户名password=1234 //数据库的密码url=jdbc:mysql://localhost:3306/jdbc //数据库的urldriver=com.mysql.cj.jdbc.Driver //驱动的位置注意是com.mysql.cj.jdbc.Driver还是com.mysql.jdbc.Driver要根据不同版本的jar包进行确定

import java.sql.DriverManager;import java.sql.Connection;@Testpublic void test4() throws Exception {//1,读取配置文件的信息//通过反射获取类加载器,然后读取properties文件,下面的ConnectionFunction是当前类的类名InputStream resourceAsStream = ConnectionFunction.class.getClassLoader().getResourceAsStream("mysql.properties");Properties properties=new Properties();//2,加载配置文件properties.load(resourceAsStream);String user = properties.getProperty("user");String password = properties.getProperty("password");String url = properties.getProperty("url");String driver = properties.getProperty("driver");//3,加载驱动Class.forName(driver);Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);}

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

考虑到以后使用jdbc时,都要获取连接,那么我们可以对上面的方法进行简单的封装,建一个类命名为JdbcUtils,封装获取连接方法的返回值是Connection,注意是sql包下的Connection,不要导错包,工具类的方法定义为static,然后把最终版的获取连接拷贝进来,最后返回这个连接就可以了。

package com.yajxweb.jdbc.utils;import com.yajxweb.jdbc.ConnectionFunction;import java.io.InputStream;import java.sql.*;import java.util.Properties;public class JdbcUtils {/*** 获取数据库连接* @return* @throws Exception*/public static Connection getConnection() throws Exception {//1,读取配置文件的信息//通过反射获取类加载器,然后读取properties文件,下面的ConnectionFunction是当前类的类名InputStream resourceAsStream = ConnectionFunction.class.getClassLoader().getResourceAsStream("mysql.properties");Properties properties=new Properties();//2,加载配置文件properties.load(resourceAsStream);String user = properties.getProperty("user");String password = properties.getProperty("password");String url = properties.getProperty("url");String driver = properties.getProperty("driver");//3,加载驱动Class.forName(driver);Connection connection = DriverManager.getConnection(url, user, password);return connection;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。