eclipse环境下,java操作MySQL的简单演示

时间:2023-03-08 15:36:29

首先先通过power shell 进入MySQL 查看现在数据库的状态(博主是win10系统)

右键开始,选择Windows powershell ,输入MySQL -u用户名 -p密码

eclipse环境下,java操作MySQL的简单演示

选择数据库(use mytest;),查看当前存在的表(show tables;),发现有多余的表,删除多余表,可以一个删除,也可以一次删除多个(drop table aaa;drop tables biao,qqq,qwde,qwe;)

eclipse环境下,java操作MySQL的简单演示

分别查看当前表内的记录

select * from cars;

select * from students;

select * from uses;

eclipse环境下,java操作MySQL的简单演示

到此,当前数据库的内容的查看完毕。

接下来打开eclipse,下面是源码:

package Test;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.Scanner; public class HelloMySQL {
static // 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名
static String url = "jdbc:mysql://127.0.0.1:3306/mytest";
// 用户名,密码
static String user = "root";
static String password = "root";
static Connection conn; public static void main(String[] args){
init();
display_main();
close(); }
/**
* 显示主菜单
*/
private static void display_main() {
Scanner sc = new Scanner(System.in);
int choose = 0;
String sql;
String sInput;
String sCarNo, sStartDate,sEndDate;
// statement用来执行SQL语句
PreparedStatement statement = null;
ResultSet rs = null;
boolean result = false; while(true){
System.out.println("请输入所选择操作的序号");
System.out.println("1:车辆信息增删查");
System.out.println("2,用车登记");
System.out.println("3,用车情况查询");
System.out.println("4,用车情况统计");
System.out.println("5,退出"); choose = sc.nextInt();
switch(choose){
case 1:
display_caroptions();
break;
case 2:
System.out.println("请分行输入要插入的车号,开始使用时间,结束使用时间:");
sc.nextLine();
sInput = sc.nextLine();
System.out.println(sInput);
String sInput1 = sc.nextLine();
System.out.println(sInput1);
String sInput2 = sc.nextLine();
System.out.println(sInput2); // 要执行的SQL语句
sql = new String("insert into uses(cno,startdate,enddate) values(?,?,?)");
System.out.println(sql);
try {
statement = conn.prepareStatement(sql);
statement.setString(1, sInput);
statement.setString(2, sInput1);
statement.setString(3, sInput2); //execute返回bool类型,表示执行结果,insert语句使用execute()
result = statement.execute();
if(!result){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} break;
case 3:
System.out.println("请输入要查询的车号:");
sc.nextLine();
sInput = sc.nextLine(); // 要执行的SQL语句
// sql = new String("select * from uses where cno = '" + sInput + "'");
sql = new String("select * from uses where cno=?");
try {
statement = conn.prepareStatement(sql);
// statement.setString(1, sInput);
statement.setString(1, "LP4200"); // 结果集rs为查询到的结果的集合,select语句使用executeQuery()
rs = statement.executeQuery(); System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println(" 车号" + "\t" + " 使用开始时间"+ " 使用结束时间"); while(rs.next()) { // 选择数据
sCarNo = rs.getString("cno");
sStartDate = rs.getString("startdate");
sEndDate = rs.getString("enddate"); // 输出结果
System.out.println(sCarNo + "\t" + sStartDate + "\t" + sEndDate);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} break;
case 4:
try {
// 要执行的SQL语句
sql = "select * from uses";
statement = conn.prepareStatement(sql);
// 结果集
rs = statement.executeQuery(); System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println(" 车号" + "\t" + " 使用开始时间" + "\t" + " 使用结束时间"); while(rs.next()) { // 选择数据
sCarNo = rs.getString("cno");
sStartDate = rs.getString("startdate");
sEndDate = rs.getString("enddate"); // 输出结果
System.out.println(sCarNo + "\t" + sStartDate + "\t" + sEndDate);
} rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} break;
case 5:
return;
default:
break;
}
} } /**
* 显示车辆信息相关操作
*/
private static void display_caroptions() {
Scanner sc = new Scanner(System.in);
int choose = 0;
int iChangeNum = 0;
String sql;
String sInput;
String sCarNo, sCarBrand,sCarType;
float fPrice = 0;
// statement用来执行SQL语句
PreparedStatement statement = null;
ResultSet rs = null;
boolean result = false;
while(true){
System.out.println("请输入所选择操作的序号");
System.out.println("1:插入车辆信息");
System.out.println("2,删除车辆信息");
System.out.println("3,查询车辆信息");
System.out.println("4,退出"); choose = sc.nextInt();
System.out.println(choose);
switch(choose){
case 1:
System.out.println("请分行输入要插入的车号,汽车品牌,汽车类型,租借每天价格:");
sc.nextLine();
sInput = sc.nextLine();
System.out.println(sInput);
String sInput1 = sc.nextLine();
System.out.println(sInput1);
String sInput2 = sc.nextLine();
System.out.println(sInput2);
fPrice = sc.nextFloat();
System.out.println(fPrice); // 要执行的SQL语句
sql = new String("insert into cars values(?,?,?,?)");
try {
statement = conn.prepareStatement(sql);
statement.setString(1, sInput);
statement.setString(2, sInput1);
statement.setString(3, sInput2);
statement.setFloat(4, fPrice);
// 结果集
result = statement.execute(); if(!result){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break; case 2: System.out.println("请输入要删除的车号:");
sc.nextLine();
sInput = sc.nextLine(); // 要执行的SQL语句
sql = new String("delete from cars where cno = ?");
try {
statement = conn.prepareStatement(sql);
statement.setString(1, sInput); // 更新数据条目,executeUpdate()返回执行SQL语句后,该表更新的记录的数目
iChangeNum = statement.executeUpdate(); if(iChangeNum != 0){
System.out.println(iChangeNum + "条记录已被删除");
}else{
System.out.println("删除失败");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break; case 3: System.out.println("请输入要查询的车号:");
sc.nextLine();
sInput = sc.nextLine(); // 要执行的SQL语句
sql = new String("select * from cars where cno=?" );
try {
statement = conn.prepareStatement(sql);
statement.setString(1, sInput);
// 结果集
System.out.println(sInput);
rs = statement.executeQuery(); System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println(" 车号" + "\t" + "品牌"+ "\t"+ "类型"+ "\t" + "每租一天价格"); while(rs.next()) { // 选择数据
sCarNo = rs.getString("cno");
sCarBrand = rs.getString("cbrand");
sCarType = rs.getString("cartype");
fPrice = rs.getFloat("cpriceperday"); // 输出结果
System.out.println(sCarNo + "\t" + sCarBrand + "\t" + sCarType + "\t" + fPrice);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} break;
case 4:
return;
default:
break;
}
}
} /**
* 初始化相关资源
*/
private static void init(){
try {
// 加载驱动程序
Class.forName(driver); // 连续数据库
conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!"); } catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
} /**
* 关闭相关资源
*/
private static void close(){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
System.out.println("感谢您的使用,再见");
}
}
}

  

接下来是测试:

eclipse环境下,java操作MySQL的简单演示

eclipse环境下,java操作MySQL的简单演示

eclipse环境下,java操作MySQL的简单演示

eclipse环境下,java操作MySQL的简单演示

以上是车辆操作的部分

接下来是用车用车相关的操作

eclipse环境下,java操作MySQL的简单演示

eclipse环境下,java操作MySQL的简单演示

eclipse环境下,java操作MySQL的简单演示