JDBC连接数据库的五种方式

配置文件jdbc.properties
user=root
password=xxxxxx1111
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&Unicode=true&characterEncoding=utf8&useSSL=false
driverClass=com.mysql.jdbc.Driver

连接数据库的五种方式
在这里插入图片描述

package jdbc_exe;

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;
import com.mysql.jdbc.Statement;

public class testConnection {
	
	@Test
	public void testConnection1(){
		try {
			Driver driver;
			driver=new com.mysql.jdbc.Driver();
			
			String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "xxxxxx1111");
			java.sql.Connection conn=driver.connect(url, info);
			System.out.println(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testConnection2() {
		try {
			Class clazz=Class.forName("com.mysql.jdbc.Driver");
			Driver driver=(Driver) clazz.newInstance();
			String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "xxxxxx1111");
			java.sql.Connection conn = driver.connect(url, info);
			System.out.println(conn);
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testConnection3(){
		try {
			Class clazz = Class.forName("com.mysql.jdbc.Driver");
			Driver driver = (Driver) clazz.newInstance();
			String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
			Properties info = new Properties();
			info.setProperty("user", "root");
			info.setProperty("password", "xxxxxx1111");
			java.sql.Connection conn = driver.connect(url, info);
			System.out.println(conn);
			
			Statement statement = (Statement) conn.createStatement();
			String sql="insert into user values(6,'XX','haha','china','11111111111')";
			int sum=statement.executeUpdate(sql);
			System.out.println("受影响的行数"+sum);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//报错找不到相应的类基本就是jar包导入的问题
	//需要把mysql-connector-java-5.1的jar包导入--build path
	@Test
	public void testConnection4(){
		try {
			String url="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8";
			String user="root";
			String password="xxxxxx1111";
			Class.forName("com.mysql.jdbc.Driver");
			
			java.sql.Connection conn = DriverManager.getConnection(url, user,password);
			System.out.println(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Test
public void getConnection5(){
		
		try {
			//1.读取配置文件中的4个基本信息
			InputStream is = testConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");
			
			Properties pros = new Properties();
			pros.load(is);
			
			String user = pros.getProperty("user");
			String password = pros.getProperty("password");
			String url = pros.getProperty("url");
			String driverClass = pros.getProperty("driverClass");
			
			//2.加载驱动
			Class.forName(driverClass);
			
			//3.获取连接
			java.sql.Connection conn = DriverManager.getConnection(url,user,password);
			System.out.println(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值