- 前提:在数据库中定义数据库mysql,表tb_user,tb_user中有三个变量id(int 主键),username(char),password(char)
- 在一个包中new 6个类(DB,Main,Query,Delete,Insert,Update)
- DB类–连接数据库,拆除数据库
package DBproject;
import java.sql.*;
public class DB { //定义一个类
public static Connection linkDB()
{
/*
连接JDBC的7步骤
1:加载驱动
2:连接数据库
3:写sql命令
4:通过sql命令获取statement对象
5:执行sql命令
6:查看sql语句执行结果
7:关闭连接
*/
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
//连接数据库mysql(包括url,user,password)//这里的A是你定义的数据库名字
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/A?useUnicode=true&characterEncoding=UTF8","root","B");//这里的B是你的数据库密码
System.out.println("连接数据库成功!");
return con;
} catch (Exception e) { //两个异常,可以选择一个总的Exception
System.out.println("连接数据库失败!");
e.printStackTrace();
return null;
}
}
public static void noconnect(ResultSet rs, PreparedStatement stmt, Connection conn)
{
if(rs!=null){ //先关数据集
try{
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){ //再关statement对象
try{
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){ //最后关闭连接
try{
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4.Main类–菜单及调用功能函数
package DBproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String []args)
{
Boolean work=true;
Connection con=null;
ResultSet rs=null;
PreparedStatement stmt=null;
Scanner scanner=new Scanner(System.in);
Scanner scan=new Scanner(System.in);
con=DB.linkDB(); //连接数据库
printmenu(); //输出菜单
while (work) {
System.out.println("请选择操作(0,1,2,3,4):");
String temp = scan.nextLine();
switch (temp) {
case "1":
Query.query(rs, stmt, con);
break;
case "2":
Insert.insert(rs, stmt, con);
break;
case "3":Update.update(rs, stmt, con);
break;
case "4":Delete.delete(rs, stmt, con);
break;
case "0":work=false; System.out.println("退出成功!");
break;
default:
break;
}
}
DB.noconnect(rs,stmt,con);//拆链接
}
public static void printmenu()
{
System.out.println("1 查询表中所有数据");
System.out.println("2 插入一条记录");
System.out.println("3 更新一条记录");
System.out.println("4 删除表中一条数据");
System.out.println("0 退出");
}
}
5.Query类–查询所有数据
package DBproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Query {
public static void query(ResultSet rs, PreparedStatement stmt, Connection con)
{
try {
String sql1="select * from tb_user"; //数据库查询语句
stmt=con.prepareStatement(sql1);//要执行SQL语句,必须获得java.sql.Statement实例
rs=stmt.executeQuery();//执行查询语句,返回数据集
while(rs.next())
{
int ID=rs.getInt("id"); //获取ID
String username=rs.getString("username"); //获取username
String password=rs.getString("password"); //获取password
System.out.println(ID+"\t"+username+"\t"+password);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6.Delete–删除一条记录
package DBproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Delete {
public static void delete(ResultSet rs, PreparedStatement stmt, Connection con)
{
Scanner scan=new Scanner(System.in);
String deletesql="delete from tb_user where id=?";//删除的sql命令
try {
stmt=con.prepareStatement(deletesql); //创建statement对象
System.out.println("输入需要删除记录的ID:");
int id=scan.nextInt();
stmt.setInt(1,id); //找到对应记录
stmt.executeUpdate(); //更新操作,实为删除
System.out.println("删除记录成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
7.Insert–插入一条记录
package DBproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Insert {
public static void insert(ResultSet rs, PreparedStatement stmt, Connection con)
{//为什么要两个,是因为int型和String类型不可共用一个,不然会出错
Scanner scanner=new Scanner(System.in);
Scanner scan=new Scanner(System.in);
String sql2="insert into tb_user(id,username,password)value(?,?,?)";
try {
stmt=con.prepareStatement(sql2);
System.out.println("输入ID");
int id=scanner.nextInt();
stmt.setInt(1,id);//插入id
System.out.println("输入username");
String username=scan.nextLine();
stmt.setString(2,username);//插入username
System.out.println("输入password");
String password=scan.nextLine();
stmt.setString(3,password);
stmt.executeUpdate();//更新表
System.out.println("插入数据成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
8。Update–更新一条记录
package DBproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Update {
public static void update(ResultSet rs, PreparedStatement stmt, Connection con)
{
Scanner scan=new Scanner(System.in);
Scanner scanner=new Scanner(System.in);
String updatesql="update tb_user set username=?,password=? where id=?";
try {
stmt=con.prepareStatement(updatesql);
System.out.println("输入需要更改数据的记录ID:");
int id=scan.nextInt();
stmt.setInt(3,id);
System.out.println("输入username");
String username=scanner.nextLine();
stmt.setString(1,username);
System.out.println("输入password");
String password=scanner.nextLine();
stmt.setString(2,password);
stmt.executeUpdate();
System.out.println("更改记录成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}