JDBC:Java使用Driver驱动程序连接MySQL数据库

Java使用Driver驱动程序连接数据库的具体实现:

  1. 创建Driver驱动程序实例
    Driver driver=new com.mysql.cj.jdbc.Driver();
  2. 准备连接数据库所需要的请求参数
    url,user,password等
  3. 使用Driver驱动的connect方法连接数据库
    driver.connect(url,info);
    下面给出两种方法,第一种比较有局限性(与MySQL紧密耦合),推荐使用第二种(通用方法,只需要修改配置文件即可连接其他数据库)

源代码

第一种

package lesson1;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import org.junit.Test;
public class JDBCtest {

	@Test
	public void test(){
		//fail("Not yet implemented");
		String url="jdbc:mysql://localhost:3306/world?serverTimezone=UTC";//url
		Properties info=new Properties();//键值对信息属性
		//输入键值对信息
		info.put("user","root");
		info.put("password","******");
		try {
			Driver driver = new com.mysql.cj.jdbc.Driver();//创建连接MySQL驱动接口
			Connection connection=driver.connect(url, info);//返回连接借口对象
			System.out.println(connection);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}
		
	}
}

第二种

package lesson1;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import org.junit.Test;
public class JDBCtest {
	public Connection getConnection()
	{
		//准备数据
		String driverClass=null;
		String url=null;
		String user=null;
		String password=null;
		Properties info=new Properties();
		Connection connection=null;//连接接口对象
		try {
			//读入配置文件数据
			FileReader fis=new FileReader("E:\\Files\\eclipse-workspace\\JDBC\\src\\lesson1\\JDBCvalue.txt");
			BufferedReader bis=new BufferedReader(fis);
			ArrayList<String> inputList=new ArrayList<String>();
			String str;
			while((str=bis.readLine())!=null)
			{
				inputList.add(str);
			}
			bis.close();
			driverClass=inputList.get(0);
			url=inputList.get(1);
			user=inputList.get(2);
			password=inputList.get(3);
			//键值对
			info.put("user",user);
			info.put("password",password);
			Driver driver=(Driver) Class.forName(driverClass).newInstance();//创建连接MySQL驱动接口
			connection = driver.connect(url, info);//返回连接接口对象
		}catch (IOException e1) {
			e1.printStackTrace();
		}catch (SQLException e) {
			System.out.println(e.getMessage());
		}catch (InstantiationException e) {
			System.out.println(e.getMessage());
		} catch (IllegalAccessException e) {
			System.out.println(e.getMessage());
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
		return connection;
	}
	@Test
	public void testgetConnection()
	{
		System.out.println(getConnection());
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玖语巴黎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值