能够实现以下四种需求:
1、添加学生信息
2、删除学生信息
3、修改学生信息
4、查询学生信息
代码展示:
main包中的Main类代码:
package com.zzu.main;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import javax.swing.tree.RowMapper;
import com.zzu.tool.db.DBLink;
import com.zzu.tool.db.IRowMapper;
public class Main {
private static DBLink db = new DBLink();
public static void main(String[] args) {
System.out.println("*********************************");
System.out.println("*\t\t\t\t*");
System.out.println("*\t欢迎使用学生信息管理系统\t*");
System.out.println("*\t\t\t\t*");
System.out.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");//地址传递
System.out.println("4、查询学生信息");//name
System.out.println("请输入操作,以Enter键结束:");
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
switch (option) {
case 1:{
System.out.println("请输入学生学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(db.exist(sql)) {
System.out.println("学号不允许重复,请重新输入");
return;
}
System.out.println("请输入学生姓名");
String name = scanner.next();
System.out.println("请输入学生电话");
String mobile = scanner.next();
System.out.println("请输入学生住址");
String address = scanner.next();
sql = "insert into student(id,name,mobile,address) values ('"+id+"','"+name+"','"+mobile+"','"+address+"')";
if(db.update(sql)) {
System.out.println("添加成功");
return;
}
System.out.println("添加失败");
break;
}
case 2:{
System.out.println("请输入学生学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exist(sql)) {
System.out.println("学号不存在,删除失败");
return;
}
sql = "delete from student where id = '"+id+"'";
db.update(sql);
System.out.println("删除成功");
break;
}
case 3:{
System.out.println("请输入学生学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exist(sql)) {
System.out.println("学号不存在,修改失败");
return;
}
System.out.println("请输入学生新姓名");
String name = scanner.next();
System.out.println("请输入学生新电话");
String mobile = scanner.next();
System.out.println("请输入学生新住址");
String address = scanner.next();
sql = "update student set name = '"+name+"',mobile = '"+mobile+"',address = '"+address+"' where id = '"+id+"'";
if(db.update(sql)){
System.out.println("修改成功");
return ;
}
System.out.println("修改失败");
break;
}
case 4:{
System.out.println("请输入学生学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exist(sql)) {
System.out.println("学号不存在,删除失败");
return;
}
sql="select id,name,mobile,address from student where id = '"+id+"'";
//有名内部类
// class RowMapper implements IRowMapper {
// @Override
// public void rowMapper(ResultSet rs) {
// try {
// while(rs.next()) {
// String name = rs.getString("name");
// String mobile = rs.getString("mobile");
// String address = rs.getString("address");
// System.out.println(id+","+name+","+mobile+","+address);
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// }
// db.select(sql, new IRowMapper());
//匿名内部类
// db.select(sql, new IRowMapper(){
// public void rowMapper(ResultSet rs) {
// try {
// while(rs.next()) {
// String name = rs.getString("name");
// String mobile = rs.getString("mobile");
// String address = rs.getString("address");
// System.out.println(id+","+name+","+mobile+","+address);
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// });
//拉姆达表达式
db.select(sql, (/*ResultSet*/ rs)-> {
try {
while(rs.next()) {
String name = rs.getString("name");
String mobile = rs.getString("mobile");
String address = rs.getString("address");
System.out.println(id+","+name+","+mobile+","+address);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
break;
}
default:
System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
}
}
}
tool包中db包中的DBLink类代码:
package com.zzu.tool.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBLink {
private Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://127.0.0.1:3306/test";
return DriverManager.getConnection(url, "root", "root");//获取连接
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void select(String sql,IRowMapper rowMapper) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类对象
Connection connection = null; //此处的rowMapper就是一个形参,到时候接口实现类里的实参再传给它
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中
rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法 多态
} catch (Exception e) {
e.printStackTrace();
}finally {
close(resultSet,statement,connection);
}
}
public boolean exist(String sql) {
Connection connection = null;
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中
return resultSet.next();
} catch (Exception e) {
e.printStackTrace();
}finally {
close(resultSet,statement,connection);
}
return false;
}
public boolean update(String sql) {
Connection connection =null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
int result = statement.executeUpdate(sql);
/*
statement.close();//如果上面代码出现异常,则该行代码及其下面代码无法执行,所以资源无法释放;比如sql语句语法错误,则statement和connection无法释放
connection.close();
*/
return result>0 ;
}catch(Exception e) {
e.printStackTrace();
}finally {
close(statement,connection);
}
return false;
}
private void close(Statement statement,Connection connection) {
try {
if(statement!=null) {//由于异常statement没有赋值,statement异常,Eg:URL出错
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void close(ResultSet resultSet,Statement statement,Connection connection) {
try {
if(resultSet!=null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
close(statement,connection);
}
}
tool包中db包中的IRowMapper代码:
package com.zzu.tool.db;
import java.sql.ResultSet;
@FunctionalInterface
public interface IRowMapper {
void rowMapper(ResultSet rs);
}
在使用DBLink类中方法时,需要添加第三方jar包,创建路径,才能完成链接数据库等步骤。