JDBC实现学生管理系统
本文实例为大家分享了JDBC实现学生管理系统的具体代码,供大家参考,具体内容如下
1、学生类
packagemanage;
importjava.util.Date;
/**
*@authorfanxf
*@since2018/4/2717:01
*/
publicclassStudent{
privateintid;
privateintage;
privateStringsex;
privateStringname;
privateDatedateCreated;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicDategetDateCreated(){
returndateCreated;
}
publicvoidsetDateCreated(DatedateCreated){
this.dateCreated=dateCreated;
}
publicStudent(){
}
publicStudent(intage,Stringsex,Stringname){
this.age=age;
this.sex=sex;
this.name=name;
}
publicStudent(intid,intage,Stringsex,Stringname){
this.id=id;
this.age=age;
this.sex=sex;
this.name=name;
}
@Override
publicStringtoString(){
return"Student{"+
"id="+id+
",age="+age+
",sex='"+sex+'\''+
",name='"+name+'\''+
",dateCreated="+dateCreated+
'}';
}
}
2、jdbc工具类
packagemanage;
importjava.io.IOException;
importjava.sql.*;
importjava.util.Properties;
/**
*@authorfanxf
*@since2018/4/2711:06
*/
//数据库的工具类
publicclassJdbcUtils{
privatestaticStringdriver="";
privatestaticStringurl="";
privatestaticStringuser="";
privatestaticStringpassword="";
static{
Propertiesp=newProperties();
try{
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
}catch(IOExceptione){
e.printStackTrace();
}
driver=p.getProperty("driver");
url=p.getProperty("url");
user=p.getProperty("user");
password=p.getProperty("password");
try{
Class.forName(driver);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
publicstaticConnectiongetConnection(){
try{
returnDriverManager.getConnection(url,user,password);
}catch(SQLExceptione){
e.printStackTrace();
}
returnnull;
}
//释放的时候要从小到大释放
//Connection->Statement-->Resultset
publicstaticvoidrelease(ResultSetrs,Statementstmt,Connectionconn){
if(rs!=null){
try{
rs.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
if(stmt!=null){
try{
stmt.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
}
3、代码
packagemanage;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.Scanner;
/**
*@authorfanxf
*@since2018/4/2717:06
*/
publicclassManageSystem{
privatestaticConnectionconn=null;
privatestaticPreparedStatementps=null;
privatestaticResultSetrs=null;
/**
*添加学生数据
*
*@paramstudent
*@return
*/
publicstaticintaddStudent(Studentstudent){
conn=JdbcUtils.getConnection();
intresult=0;
try{
ps=conn.prepareStatement("INSERTINTOstudent(age,sex,`name`,dateCreated)VALUES(?,?,?,now())");
ps.setInt(1,student.getAge());//设置第一个参数
ps.setString(2,student.getSex());//设置第二个参数
ps.setString(3,student.getName());//设置第三个参数
result=ps.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
JdbcUtils.release(null,ps,conn);//关闭连接
}
returnresult;
}
publicvoidadd(){
Scannerscan=newScanner(System.in);
System.out.println("请输入学生年龄");
intage=scan.nextInt();
System.out.println("请输入学生性别");
Stringsex=scan.next();
System.out.println("请输入学生姓名");
Stringname=scan.next();
Students=newStudent(age,sex,name);
intflag=addStudent(s);
if(flag>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
/**
*修改
*
*@paramstudent
*@return
*/
publicstaticintupdateStudent(Studentstudent){
conn=JdbcUtils.getConnection();
intresult=0;
try{
ps=conn.prepareStatement("UPDATEstudentSETage=?,sex=?,`name`=?WHEREid=?");
ps.setInt(1,student.getAge());//设置第一个参数
ps.setString(2,student.getSex());//设置第二个参数
ps.setString(3,student.getName());//设置第三个参数
ps.setInt(4,student.getId());
result=ps.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
JdbcUtils.release(null,ps,conn);//关闭连接
}
returnresult;
}
publicvoidupdate(){
Scannerscan=newScanner(System.in);
System.out.println("请输入学生id");
intid=scan.nextInt();
System.out.println("请输入学生年龄");
intage=scan.nextInt();
System.out.println("请输入学生性别");
Stringsex=scan.next();
System.out.println("请输入学生姓名");
Stringname=scan.next();
Students=newStudent(id,age,sex,name);
intflag=updateStudent(s);
if(flag>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
}
/**
*删除
*
*@paramid
*@return
*/
publicstaticintdeleteStudent(intid){
conn=JdbcUtils.getConnection();
intresult=0;
try{
ps=conn.prepareStatement("DELETEFROMstudentWHEREid=?");
ps.setInt(1,id);//设置第一个参数
result=ps.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
JdbcUtils.release(null,ps,conn);//关闭连接
}
returnresult;
}
publicvoiddelete(){
Scannerscan=newScanner(System.in);
System.out.println("请输入学生id");
intid=scan.nextInt();
intflag=deleteStudent(id);
if(flag>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
publicstaticvoidmain(String[]args){
System.out.println("************欢迎进入学生管理系统*************");
ManageSystemms=newManageSystem();
booleanb=true;
while(b){
System.out.println("你想进行以下哪项操作");
System.out.println("1、添加学生2、更新学生数据3、学生信息查询4、删除学生0、退出");
Scannerscan=newScanner(System.in);
inti=scan.nextInt();
switch(i){
case1:
ms.add();
break;
case2:
ms.update();
break;
case3:
System.out.println();
break;
case4:
ms.delete();
break;
default:
System.out.println("没有该操作选项,请重新来过!");
main(args);
break;
}
}
}
}
4、properties数据库文件自己配置
数据库字段根据学生类建立!
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。