创建数据库:
然后eclipse基本框架是:
需要添加jar包:
log4j jar:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependencyservlet:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>将上面的包加在:
其余代码上个博客上面有,可以去找找看。
main java:
package com.zzu.client; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; import com.zzu.tool.db.DBLink; import com.zzu.tool.db.IRowMapper; public class Main { 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(); DBLink db = new DBLink(); switch (option) { case 1:{ System.out.println("请输入学号:"); String id=scanner.next(); System.out.println("请输入姓名:"); String name=scanner.next(); System.out.println("请输入电话号码:"); String mobile=scanner.next(); System.out.println("请输入家庭住址:"); String address=scanner.next(); String sql="select id from user_info where id=?"; if(db.exist(sql,id)) { System.out.println("学号已存在,请重新操作"); return; } sql="insert into user_info (id,name,mobile,address) values (?,?,?,?)"; if(db.update(sql, id,address,name,mobile)) { System.out.println("注册成功!"); return; } break; } case 2:{ System.out.println("请输入学号,进行修改"); String id =scanner.next(); String sql="select id from user_info where id=?"; if(!db.exist(sql, id)) { System.out.println("学号不存在,请重新操作!"); return; }//这里也可以优化一下,把这段if省去也可以 sql="delete from user_info where id=? "; if(db.update(sql, id)) { System.out.println("删除成功!"); return; } System.out.println("删除失败!"); break; } case 3:{ System.out.println("请输入学号,进行修改"); String id =scanner.next(); String sql="select id from user_info where id=?"; if(!db.exist(sql, id)) { 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 user_info set id=?, name =?,mobile=?,address=? "; if(db.update(sql, id,name,mobile,address)) { System.out.println("修改成功!"); return; } System.out.println("修改失败!"); break; } case 4:{ System.out.println("请输入学号:"); String id =scanner.next(); String sql = "select id,name,mobile,address from user_info where id = ?"; //形式一: /*class RowMapper implements IRowMapper{ public void rowMapper(ResultSet rs) { try { if(rs.next()) { //String id =rs.getString("id");//两者id相同,没必要要了, //如果是jdk8之前要在case4 中的String id =scanner.next();变为 final String id =scanner.next(); //8以后final可以省略 String name =rs.getString("name"); String mobile =rs.getString("mobile"); String address =rs.getString("address"); System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+"."); return; } System.out.println("未找到信息!"); } catch (SQLException e) { e.printStackTrace(); } } } IRowMapper rowMapper=new RowMapper();//上转型对象 db.select(sql, rowMapper,id); */ //lambda表达式形式: /*IRowMapper rowMapper=(rs) ->{ try { if(rs.next()) { String name =rs.getString("name"); String mobile =rs.getString("mobile"); String address =rs.getString("address"); System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+"."); return; } System.out.println("未找到信息!"); } catch (SQLException e) { e.printStackTrace(); } }; db.select(sql, rowMapper,id);*/ //形式三: db.select(sql, (rs)->{ try { if(rs.next()) { String name =rs.getString("name"); String mobile =rs.getString("mobile"); String address =rs.getString("address"); System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+"."); return; } System.out.println("未找到信息!"); } catch (SQLException e) { e.printStackTrace(); } }, id); break; } default: System.out.println("I'm Sorry,there is not the "+option+" option,please try again."); System.exit(0); } } }
其中,在查询时jdk升级时要在pom.xml中加入以下:
<profiles> <profile> <id>jdk1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> </profiles>然后进行更新:
这样就ok了,可以不用加final了。
