实训第2天的主要任务是使用jdbc技术连接MySQL数据库,对数据库表中的数据实现增、删、改、查等基本功能。
1.加载驱动
2.建立连接
3.写sql语句
4.得到statement对象
5.执行sql,得到结果集
6.处理结果集
7.关闭资源
下面是具体的实现过程:
(1)打开ieda,选择Create New Project,新建一个项目;
(2)选择Java,Project SDK框选配置好的jdk,点击Next;
(3)不勾选Create project from template,点Next;
(4)填写项目名,选择项目地址,点击Finish。
(5)打开SQLyog,新建数据库(我的是idea),在数据库中新建表(userinfo),表的设置如下:
(6)先手动输入几个数据,在后面会用到;
(7)在idea的项目名处右键->New->Directory,创建Directory:lib
(8)把mysql-connector-java-5.1.36.jar资源包移入lib目录下,选中资源包右键->Add as Library->OK;
(9)在src目录下新建Package(我的命名:com.song)
(10)在song目录下新建class:UserInfo
package com.song;
public class UserInfo {
public UserInfo(String username, String password){
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
@Override
public String toString(){
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(11)在song目录下新建class:query(注意在第2步建立连接时,设置为自己数据库名、用户名、密码)
package com.song;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class query {
public static void main(String[] args){
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List userInfos = new ArrayList<>();
try{
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/idea?useSSL=true&"
+ "characterEncoding=utf-8&user=root&password=root");
//3.写sql
String sql = "select * from userinfo";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql,得到结果集
resultSet = statement.executeQuery();
//6.处理结果集
while(resultSet.next()){
int id = resultSet.getInt(1);
String username = resultSet.getString(2);
String password = resultSet.getString(3);
UserInfo userInfo = new UserInfo(username,password);
userInfo.setId(id);
userInfos.add(userInfo);
}
System.out.println(userInfos);
//7.关闭资源
}catch(Exception e){
e.printStackTrace();
}finally {
if(resultSet != null){
try{
resultSet.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try{
connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
(12)这时已经实现了查询功能,运行query里的主函数,可以输出查询结果;
(13)同理实现增、删、改功能。新建class:add
package com.song;
import java.sql.*;
public class add {
public static void main(String[] args){
Connection connection = null;
PreparedStatement statement = null;
try {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/idea?useSSL=true&"
+ "characterEncoding=utf-8&user=root&password=root");
//3.写SQL
String sql = "insert into userinfo(id,username,password) values(?,?,?)";
//4.得到statement对像
statement = connection.prepareStatement(sql);
//添加的用户信息
statement.setInt(1,5);
statement.setString(2,"song");
statement.setString(3,"song");
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
//7.关闭资源
}catch(Exception e){
e.printStackTrace();
}finally {
if(statement != null){
try{
statement.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try{
connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
刷新数据库,查看运行结果
(14)新建class:delete
package com.song;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class delete {
public static void main(String[] args){
Connection connection = null;
PreparedStatement statement = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/idea?useSSL=true&"
+ "characterEncoding=utf-8&user=root&password=root");
//3.写sql
String sql = "delete from userinfo where id=5"; //删除的id
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
//7.关闭资源
}catch(Exception e){
e.printStackTrace();
}finally {
if(statement != null){
try {
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(connection != null){
try{
connection.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
刷新数据库,查看运行结果
(15)新建class:update
package com.song;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class update {
public static void main(String[] args){
Connection connection = null;
PreparedStatement statement = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/idea?useSSL=true&"
+ "characterEncoding=utf-8&user=root&password=root");
//3.写SQL
String sql = "update userinfo set username='SONG',password='SONG' where id=1";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
//7.关闭资源
}catch(Exception e){
e.printStackTrace();
}finally {
if(statement!=null){
try{
statement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(connection!=null){
try{
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}