文章简介
本文章讲解使用mysql-connector-java-8.0.30建立java程序和mysql本地数据库的连接。
自用文章,仅作参考。
库
mysql-connect-java-8.0.30 (来自Maven)
IDE:IntelliJ IDEA
安装方法(任选其一)
通过IDEA项目结构安装
1.打开项目的项目结构(Project Structure)
2.在弹出的窗口选择 “库(Libraries)”-> “+” ->“来自Maven(From Maven)”
3.输入 mysql-connector-java,
并点击搜索开始搜索,
等待搜索结果后展开搜索结果,
选择mysql:mysql-connector-java:8.0.30
(建议、可选)勾选"下载到(Download to)
4.至此,外部库下载完成。
通过外部链接下载并添加
1.进入Maven官网:Maven 存储库:搜索/浏览/浏览 — Maven Repository: Search/Browse/Explore (mvnrepository.com)
2.顶部搜索框输入mysql-connector-java,回车搜索。得到结果与下图类似,根据相关信息选择第二个
3.根据自身需求选择下载版本,本文章使用8.0.30版本,点击版本号进入页面
4.根据自身需求选择下载jar包或者添加依赖,本文章选择下载jar包,添加依赖不需要再下载jar包
5.回到IDEA,在项目下创建一个"lib"目录,目录名字可自取,建议"lib"存放外部库。
将下载好的jar包粘贴至"lib"目录,右键jar包选择"添加为库(Add as Library …)"
弹出的窗口再选择 “OK” 即可
6.至此,添加完成。
使用方法
导入
import com.mysql.cj.jdbc.MysqlDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;连接mysql数据库
DataSource dataSource = new MysqlDataSource();//创建数据库资源对象 //强制转换,原因自查 setUrl()参数自定义,对于windows系统一般格式是: //jdbc:mysql://localhost:3306/创建的数据库名,此处我创建了一个名为accounts的数据库(creat database) ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://localhost:3306/accounts");设置连接数据库的用户名即密码(数据库的用户名和密码在安装时已设置)
((MysqlDataSource) dataSource).setUser("root");//此处设置用户名,windows系统默认root ((MysqlDataSource) dataSource).setPassword("50314");//密码在安装mysql时已经设置建议连接,获得连接需要处理SQLException异常
try { Connection connection = dataSource.getConnection();//获得与数据库的连接 } catch (SQLException e) { e.printStackTrace(); }使用预处理语句
创建一个预处理对象
PreparedStatement preparedStatement;定义一个String变量,用于存储mysql的代码,如下所示
命令执行的是"select * from accounts where account_name=" + username +" and account_password=" + password;
输入mysql命令时请确保无任何符号遗漏
String sql = "select * from accounts where account_name=" + username +" and account_password=" + password;执行命令,并获得结果
try { preparedStatement = connection.prepareStatement(sql);//预处理 ResultSet resultSet = preparedStatement.executeQuery();//执行处理并返回结果 }catch (SQLException e) { e.printStackTrace(); }现在上述resultSet存储了来自mysql筛选后的结果。
本章示例完整代码:(演示如何在数据库中是否保存有某个账号)
package database; import com.mysql.cj.jdbc.MysqlDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class AccountDataBase { private PreparedStatement preparedStatement; private Connection connection; private static AccountDataBase accountDataBase; private AccountDataBase() { preparedStatement = null; DataSource dataSource = new MysqlDataSource(); ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://localhost:3306/accounts"); ((MysqlDataSource) dataSource).setUser("root"); ((MysqlDataSource) dataSource).setPassword("50314"); try { connection = dataSource.getConnection(); preparedStatement = connection.prepareStatement("insert into account values(?,?,?,?)"); } catch (SQLException e) { e.printStackTrace(); } } public boolean checkAccount(String username, String password) { String sql = "select * from accounts where account_name=" + username +" and account_password=" + password; try { preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return true; } }catch (SQLException e) { e.printStackTrace(); } return false; } public static AccountDataBase getAccountDataBase() { accountDataBase = new AccountDataBase(); return accountDataBase; } }总结
本文章简单讲解了如何在java中直接使用mysql的语法筛选数据库信息。