博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java通过JDBC连接Mysql数据库的方法和实例
阅读量:2442 次
发布时间:2019-05-10

本文共 3348 字,大约阅读时间需要 11 分钟。

写了一个JDBC连接数据库的代码特别高兴的来和大家分享一下

Java是通过JDBC连接Mysql数据库的。JDBC(Data Base Connectivity,java连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使开发人员能够编写数据库

第一步

大家一定要记住导入连接mysql的驱动jar文件,下图

第二步

我直接给大家上我的代码了哈,给大家看看。笔记都写在代码的注释里面了,大家可以看得懂得!

public class JDBCUtils {	/*	 * 实现jdbc的工具类 定义方法,直接返回数据库的连接对象	 */	private JDBCUtils() {	}	private static Connection con;	static {		// 1,注册驱动		// 使用java.sql.DriverManger类静态方法registerDriver(Driver driver)		// Diver 是一个接口,传递参数,将mysql驱动程序的实现类		// 把一个类加载进内存		try {			Class.forName("com.mysql.jdbc.Driver");			// 让Driver被类加载			// 2,获取数据库的连接			/*			 * static Connection getConnection(String url,String user,String			 * password), 返回值的Connection接口的实现类,在mysql驱动中 url: 数据库连接地址			 * jdbc:mysql://localhost:3306//数据库名 username:用户名 password:密码			 * 			 */			String url = "jdbc:mysql://localhost:3306/test01";			String username = "root";			String password = "root";			// DriverManager.getConnection(url, username, password);来实现字符串的连接			// 面向接口编程 ,左边的对象是接口			con = DriverManager.getConnection(url, username, password);		} catch (Exception e) {			// TODO Auto-generated catch block			throw new RuntimeException(e + "数据库连接失败");		}	}	// 对外提供连接数据库的方法	public static Connection getConnection() {		return con;	}}

第三部

直接调用这一封装好的方法就可以连接到数据库了。

public class QueryRunnerDemo01 {	//直接调用JDBCUtils对象中写好的getConnection()方法	private static Connection con=JDBCUtils.getConnection();		public static void main(String[] args) throws SQLException {		// TODO Auto-generated method stub		//ArrayHandlermethod_01();		//ArrayListHandlermethod_02();		//BeanHandlermethod_03();		//BeanListHandlermethod_04();		//ColumnListHandlermethod_05();		//ScalarHandlermethod_06();		//mapHandlermethod_07() ;		mapListHandlermentod_08();	}

最后和大家谈一谈可能出现的一些异常

Note:如果你没有添加jar程序驱动包的话编译的时候发现以下问题:

Sorry,can`t find the Driver!

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    atjava.URLClassLoader$1.run(Unknown Source)

    atjava.URLClassLoader$1.run(Unknown Source)

    atjava.security.AccessController.doPrivileged(Native Method)

    atjava.net.URLClassLoader.findClass(Unknown Source)

    atjava.lang.ClassLoader.loadClass(Unknown Source)

    atsun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

    atjava.lang.ClassLoader.loadClass(Unknown Source)

    atjava.lang.Class.forName0(Native Method)

     atjava.lang.Class.forName(Unknown Source)


这时,你只需要按照里的方法就可以解决。

但是编译运行虽然成功,但是有以下的warning信息:

Thu Dec 24 00:08:37 CST 2015WARN: Establishing SSL connection without server's identity verification is notrecommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSLconnection must be established by default if explicit option isn't set. Forcompliance with existing applications not using SSL the verifyServerCertificateproperty is set to 'false'. You need either to explicitly disable SSL bysetting useSSL=false, or set useSSL=true and provide truststore for servercertificate verification.

Succeeded connecting to theDatabase!

AFG Kabul

AFG Qandahar

AFG Herat

AFG Mazar-e-Sharif

NLD Amsterdam

NLD Rotterdam

NLD Haag

这时只需要把url字符串改为以下:

改之前:String url ="jdbc:mysql://127.0.0.1:3306/world";

改之后:String url = "jdbc:mysql://127.0.0.1:3306/world?useUnicode=true&characterEncoding=utf-8&useSSL=false";

就发现warning没了,程序运行成功。

Succeeded connecting to theDatabase!

AFG Kabul

AFG Qandahar

AFG Herat

AFG Mazar-e-Sharif

NLD Amsterdam

NLD Rotterdam

NLD Haag

NLD                                 Utrecht

你可能感兴趣的文章
gearman 任务失败_Gearman简介-PHP中的多任务
查看>>
成为PHP专业人员:缺少的链接
查看>>
了解OpCache
查看>>
symfony 2_使用Symfony 2:构建Web应用程序
查看>>
0xDBE:初步了解
查看>>
使用Phake自动化PHP-简介
查看>>
livereload_LiveReload
查看>>
vs2013项目迁移不成功_浏览器趋势2013年9月:迁移到Microsoft?
查看>>
如何在Windows上安装Ghost
查看>>
Magento电子商务网站的SEO指南
查看>>
用Mockery模拟您的测试依赖项
查看>>
Coderbits来自哪里?
查看>>
phpstorm -xmx_PhpStorm 8-新功能
查看>>
Chrome 27的新功能
查看>>
播客-SitePoint的新产品…
查看>>
浏览器趋势(2013年5月):IE8降至10%以下
查看>>
防弹您的Drupal网站
查看>>
采访:汤姆·奥兰(Tom Oram)和罗伯·艾伦(Rob Allen)
查看>>
将Solarium与SOLR结合使用进行搜索-Solarium和GUI
查看>>
ie浏览器报错:不支持此类_浏览器趋势(2013年3月):IE降至30%以下
查看>>