JDBC四种连接Mysql方式以及一些错误【做个笔记1】

JDBC

JDBC连接错误

在学习jdbc时遇到如下困难
在这里插入图片描述

java.sql.SQLException: Unknown initial character set index ‘255’
received from server. Initial client character set can be forced via
the ‘characterEncoding’ property.

提示在连接数据库时增加编码

将url改成如下形式

String url = "jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8";

一开始我改成如下代码,报错表示不认识这个数据库

String url = "jdbc:mysql://localhost:3306/test useUnicode=true&characterEncoding=utf8";

后来在数据库后面加了一个问号? 就能正常连接了
在这里插入图片描述

JDBC五种连接方式

package com.atguigu.connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

public class ConnectionTest {
    
    

	// 方式一
	@Test
	public void testConnection1() throws SQLException {
    
    
		// 1. 获取Driver实现类对象
		Driver driver = new com.mysql.jdbc.Driver();

		// test 数据库名
		String url = "jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8";
		// 将mysql用户名和密码封装在Properties中
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "abc");

		Connection conn = driver.connect(url, info);

		System.out.println(conn);

	}

	// 方式二:方式一的迭代:不出现第三方的api,使之有更好的移植性
	@Test
	public void testConnection2() throws Exception {
    
    

		// 1. 获取Driver实现类对象 利用反射
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();

		// 2.提供要连接的数据库
		// test 数据库名
		String url = "jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8";

		// 3.提供要连接的用户名和密码
		// 将mysql用户名和密码封装在Properties中
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password", "abc");

		// 4.获取连接
		Connection conn = driver.connect(url, info);
		System.out.println(conn);

	}

	// 方式三:使用DriverManager替换Driver
	@Test
	public void testConnection3() throws Exception {
    
    

		// 1. 获取Driver实现类对象 利用反射
		Class clazz = Class.forName("com.mysql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();

		// 2.提供另外三个连接的信息
		String url = "jdbc:mysql://localhost:3306/test? " + "useUnicode=true&characterEncoding=utf8";
		String user = "root";
		String password = "abc";

		// 注册驱动
		DriverManager.registerDriver(driver);

		// 获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
	}

	// 方式四:可以只加载驱动,不用注册驱动
	@Test
	public void testConnection4() throws Exception {
    
    

		// 1.提供另外三个连接的信息
		String url = "jdbc:mysql://localhost:3306/test? " + "useUnicode=true&characterEncoding=utf8";
		String user = "root";
		String password = "abc";

		// 2.加载驱动
		Class.forName("com.mysql.jdbc.Driver");

		// 可以省略如下操作 ,原因如下
		// Driver driver = (Driver) clazz.newInstance();
		// 注册驱动
		// DriverManager.registerDriver(driver);

		/*
		 * static { try { java.sql.DriverManager.registerDriver(new Driver()); }
		 * catch (SQLException E) { throw new
		 * RuntimeException("Can't register driver!"); } }
		 */

		// 获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
	}

}

jdbc.properties


user=root
password=abc
url=jdbc:mysql://localhost:3306/test?  useUnicode=true&characterEncoding=utf8
driverClass=com.mysql.jdbc.Driver
	

	
@Test
	public void testConnection5() throws Exception {
    
    
		InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

		Properties properties = new Properties();
		properties.load(is);

		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String url = properties.getProperty("url");
		String driverClass = properties.getProperty("driverClass");

		Class.forName(driverClass);

		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);

	}

猜你喜欢

转载自blog.csdn.net/weixin_46689011/article/details/120321372