import java.sql.*;
import java.util.Scanner;
public class HomeWork {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("001班级管理系统");
System.out.println("1.添加 2.删除 3.修改 4.查询");
System.out.print("请选择:");
int choose=input.nextInt();
switch(choose){
case 1:
add();
break;
case 2:
delete();
break;
case 3:
update();
break;
case 4:
select();
break;
default:
System.out.println("谢谢使用");
System.exit(0);
break;
}
}
public static void add(){
try {
System.out.println("请输入添加名字:");
String name=input.next();
//使用Class.forName()加载驱动
Class.forName("com.mysql.jdbc.Driver");
//数据库连接
try {
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","admin");
//获得数据库连接
String sql="INSERT grade(gradeName) VALUES (?);";
//预准备的对象
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, name);
//结果
int rs=ps.executeUpdate();
if(rs>0){
System.out.println("添加完成");
select();
}else{
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void delete(){
System.out.println("请输入年级编号:");
int number=input.nextInt();
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection con
=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","admin");
String sql="DELETE FROM grade WHERE gradeID=?;";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, number);
//返回受影响的行数
int count=ps.executeUpdate();
if(count>0){
System.out.println("删除成功");
select();
}else{
System.out.println("失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void update(){
try {
System.out.println("请输入年级编号:");
int gid=input.nextInt();
System.out.println("请输入年级名称:");
String gname=input.next();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//数据库连接
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","admin");
//
String sql="update grade set gradename=? where
gradeid=?";
//将sql发送到数据库
PreparedStatement ps=con.prepareStatement(sql);
//占位
ps.setString(1,gname );
ps.setInt(2, gid);
int count=ps.executeUpdate();
if(count>0){
System.out.println("成功");
select();
}else{
System.out.println("失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void select(){
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","admin");
//编写sql语句
String sql ="select gradeid,gradeName from grade";
//将sql发送到数据库
PreparedStatement ps=con.prepareStatement(sql);
//返回查询结果
ResultSet rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}