java实现科研信息管理系统
程序员文章站
2022-05-03 10:25:55
一、前言
本学期学习了java语言,在学期的结束,写一个有操作界面,与数据库关联的管理系统,用来巩固自己本学习所学的知识。
用到的知识:java基础,java界...
一、前言
本学期学习了java语言,在学期的结束,写一个有操作界面,与数据库关联的管理系统,用来巩固自己本学习所学的知识。
用到的知识:java基础,java界面设计(gui),oracle数据库(需要掌握数据库的基本操作语句),链接数据库。
使用的开发工具:myeclipse professional 2014
二、设计
我们管理的属性有:项目编号,项目名称,参与人员,负责人,项目开始时间,结束时间。科研项目系统主要有四个功能,对科研项目的增加、删除、修改、查询。以及为增加系统安全性所设计的登陆模式。
2.1 增加:向数据库的表中增加科研项目的所有信息
添加后在控制台使用sql语句查找,验证是否已添加至数据库中。
2.2 查询:通过具有唯一性的项目编号查找该项目的所有信息
2.3 修改:根据项目编号选中要修改的项目,并重新输入项目信息进行修改
2.4 删除:通过具有唯一性的项目编号删除对应项目的所有信息
三、窗体源码
3.1 登录界面
package 科研信息管理系统; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class loginwindows extends frame implements windowlistener,actionlistener { public label lglabel; //用户名标签 public label pwdlabel; //密码标签 public textfield lgtext; //用户名文本框 public textfield pwdtext; //密码文本框 public button *; //登录按钮 public button quitbt; //退出按钮 public loginwindows() { super(); this.setsize(400, 300); this.settitle("科研信息管理系统"); this.setlayout(null); lglabel=new label(); lglabel.settext("登录账号:"); lglabel.setsize(60, 30); lglabel.setlocation(70,70); pwdlabel=new label(); pwdlabel.settext("密 码:"); pwdlabel.setsize(60,30); pwdlabel.setlocation(70, 150); lgtext=new textfield(); lgtext.setsize(180, 30); lgtext.setlocation(140, 70); pwdtext=new textfield(); pwdtext.setsize(180,30); pwdtext.setlocation(140, 150); *=new button(); *.setlabel("登录"); *.setsize(60,30); *.setlocation(120, 220); quitbt=new button(); quitbt.setlabel("退出"); quitbt.setsize(60,30); quitbt.setlocation(220,220); quitbt.addactionlistener(this); *.addactionlistener(this); this.addwindowlistener(this); this.add(lglabel); this.add(pwdlabel); this.add(lgtext); this.add(pwdtext); this.add(*); this.add(quitbt); this.setvisible(true); } public static void main(string args[]) { loginwindows main=new loginwindows(); } @override public void actionperformed(actionevent e) { button bt=(button) e.getsource(); if(bt.getlabel().equals("退出")) { system.exit(0); } else { if ((lgtext.gettext().equals(""))||(pwdtext.gettext().equals(""))) { joptionpane.showmessagedialog(this,"账号或密码为空"); } else { if ((lgtext.gettext().equals("admin"))&&(pwdtext.gettext().equals("111"))) //if((lgtext.gettext().equals(""))||(pwdtext.gettext().equals(""))) { this.setvisible(false); // sqlwindow sql=new sqlwindow(); windowsview w=new windowsview(); w.sciencepro(); } else { joptionpane.showmessagedialog(this, "没有权限"); } }}} @override public void windowopened(windowevent e) { // todo auto-generated method busb } @override public void windowclosing(windowevent e) { // todo auto-generated method busb system.exit(0); } @override public void windowclosed(windowevent e) { // todo auto-generated method busb } @override public void windowiconified(windowevent e) { // todo auto-generated method busb } @override public void windowdeiconified(windowevent e) { // todo auto-generated method busb } @override public void windowactivated(windowevent e) { // todo auto-generated method busb } @override public void windowdeactivated(windowevent e) { // todo auto-generated method busb } }
3.2主窗体源码
package 科研信息管理系统; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.tree.*; public class windowsview implements treeselectionlistener,actionlistener //窗口类 { jframe main; jpanel leftpa; jpanel uppa; jpanel downpa; ///查询用控件 jlabel numla; //通过项目编号查询 jtextfield numtxt; jbutton numbt; jtextfield nametxt; //显示项目名称的文本框 jtextfield peopletxt; //显示参与人员 jtextfield principaltxt; //显示主要负责人 jtextfield timestarttxt; //显示开始时间的文本框 jtextfield timeendtxt; //显示预期结束时间的文本框 //增加用控件 jlabel anumla; jlabel anamela; jlabel apeoplela; jlabel aprincipalla; jlabel atimestartla; jlabel atimeendla; jtextfield anumtxt; jtextfield anametxt; jtextfield apeopletxt; jtextfield aprincipaltxt; jtextfield atimestarttxt; jtextfield atimeendtxt; jbutton addbt; jtable showtable; //删除用控件 jlabel dnumla; jbutton dnumbt; jtextfield dnumtxt; //修改用控件 jlabel alnumla; jtextfield allnumtxt; jbutton alsebt; jbutton albt; jlabel allnumla; jlabel alnamela; jlabel alpeoplela; jlabel alprincipalla; jlabel altimestartla; jlabel altimeendla; jtextfield alnumtxt; jtextfield alnametxt; jtextfield alpeopletxt; jtextfield alprincipaltxt; jtextfield altimestarttxt; jtextfield altimeendtxt; public void sciencepro() { main=new jframe(); main.setsize(800,800); main.settitle("科研信息管理"); main.setlayout(null); leftpa=new jpanel(); leftpa.setsize(150, 600); leftpa.setlocation(0, 0); leftpa.setbackground(color.white); initleftpanel(); main.add(leftpa); uppa=new jpanel(); uppa.setsize(650, 400); uppa.setlocation(150, 0); uppa.setbackground(color.gray); main.add(uppa); downpa=new jpanel(); downpa.setsize(650, 400); downpa.setlocation(150, 400); downpa.setbackground(color.orange); main.add(downpa); main.setvisible(true); } private void initleftpanel() { string[] strs={"查询","增加","删除","修改"}; jtree tree=new jtree(strs); tree.addtreeselectionlistener(this); leftpa.add(tree); } public void valuechanged(treeselectionevent e) { jtree tree=(jtree)e.getsource(); defaultmutabletreenode selectionnode =(defaultmutabletreenode)tree.getlastselectedpathcomponent(); string str=selectionnode.tostring(); if(str.equals("查询")) { initupdownpawhensearch(); } if(str.equals("增加")) { initupdownpawhenadd(); } if(str.equals("删除")) { initupdownpawhendelete(); } if(str.equals("修改")) { initupdownpawhenalert(); } } private void initupdownpawhensearch() { //清空上下面板上的控件 uppa.removeall(); downpa.removeall(); //动态的加载上面板的控件 uppa.setlayout(null); numla=new jlabel(); numla.settext("请输入项目编号"); numla.setlocation(40, 60); numla.setsize(100, 40); numtxt=new jtextfield(); numtxt.setlocation(180, 60); numtxt.setsize(200, 30); numbt=new jbutton(); numbt.settext("查询"); numbt.addactionlistener(this); numbt.setlocation(250, 160); numbt.setsize(60, 30); uppa.add(numla); uppa.add(numtxt); uppa.add(numbt); uppa.validate(); uppa.repaint(); numbt.addactionlistener(this); //动态的加载下面板的控件 nametxt=new jtextfield(); nametxt.setlocation(80, 50); nametxt.setsize(300, 30); peopletxt=new jtextfield(); peopletxt.setlocation(80, 100); peopletxt.setsize(300, 30); principaltxt=new jtextfield(); principaltxt.setlocation(80, 150); principaltxt.setsize(300, 30); timestarttxt=new jtextfield(); timestarttxt.setlocation(80, 200); timestarttxt.setsize(300, 30); timeendtxt=new jtextfield(); timeendtxt.setlocation(80,250); timeendtxt.setsize(300, 30); downpa.setlayout(null); downpa.add(nametxt); downpa.add(peopletxt); downpa.add(principaltxt); downpa.add(timestarttxt); downpa.add(timeendtxt); downpa.validate(); downpa.repaint(); } public void actionperformed(actionevent e) { jbutton bt=(jbutton)e.getsource(); //bt.addactionlistener(this); if(bt.gettext().equals("查询")) { if(numtxt.gettext().equals("")) { joptionpane.showmessagedialog(null, "请输入项目编号"); } else { dealsearch deal=new dealsearch(); string inf=deal.findprobynum(integer.parseint(numtxt.gettext().trim())); if((inf!=null)&&(!inf.equals(""))) { string[] strs=inf.split(","); nametxt.settext(strs[1]); peopletxt.settext(strs[2]); principaltxt.settext(strs[3]); timestarttxt.settext(strs[4]); timeendtxt.settext(strs[5]); } } } if(bt.gettext().equals("新增")) { if (anumtxt.gettext().equals("")||anametxt.gettext().equals("")||apeopletxt.gettext().equals("")||aprincipaltxt.gettext().equals("")||atimestarttxt.gettext().equals("")||atimeendtxt.gettext().equals("")) { joptionpane.showmessagedialog(null, "输入中不能有空值!!"); } else { scienceproject s=new scienceproject(); s.setnum(integer.parseint(anumtxt.gettext())); s.setname(anametxt.gettext()); s.setpeople(apeopletxt.gettext()); s.setleader(aprincipaltxt.gettext()); s.settimestart(atimestarttxt.gettext()); s.settimefinish(atimeendtxt.gettext()); dealadd deal=new dealadd(); deal.add(s); } } if(bt.gettext().equals("删除")) { if(integer.parseint(dnumtxt.gettext())==0) { joptionpane.showmessagedialog(null, "不能删除空的项目编号!!"); } else { dealdelete deal=new dealdelete(); deal.delete(integer.parseint(dnumtxt.gettext().trim())); } } if(bt.gettext().equals("提交")) { if(integer.parseint(allnumtxt.gettext())==0) { joptionpane.showmessagedialog(null, "不能修改空的项目编号!!"); } else { dealsub deal=new dealsub(); deal.submit(integer.parseint(allnumtxt.gettext())); } } if(bt.gettext().equals("修改")) { if (alnametxt.gettext().equals("")||alpeopletxt.gettext().equals("")||alprincipaltxt.gettext().equals("")||altimestarttxt.gettext().equals("")||altimeendtxt.gettext().equals("")) { joptionpane.showmessagedialog(null, "输入中不能有空值!!"); } else { scienceproject s=new scienceproject(); s.setnum(integer.parseint(allnumtxt.gettext())); s.setname(alnametxt.gettext()); s.setpeople(alpeopletxt.gettext()); s.setleader(alprincipaltxt.gettext()); s.settimestart(altimestarttxt.gettext()); s.settimefinish(altimeendtxt.gettext()); dealalter deal=new dealalter(); deal.alter(s); } } } private void initupdownpawhenadd() { //清空上下面板上的控件 uppa.removeall(); downpa.removeall(); //动态的加载上面板的控件 uppa.setlayout(null); anumla=new jlabel(); anumla.settext("请输入要增加的项目编号"); anumla.setlocation(30, 50); anumla.setsize(150, 40); anumtxt=new jtextfield(); anumtxt.setlocation(200, 50); anumtxt.setsize(250, 30); anamela=new jlabel(); anamela.settext("请输入要增加的项目名称"); anamela.setlocation(30, 100); anamela.setsize(150, 40); anametxt=new jtextfield(); anametxt.setlocation(200, 100); anametxt.setsize(250, 30); apeoplela=new jlabel(); apeoplela.settext("请输入项目参与人员"); apeoplela.setlocation(30, 150); apeoplela.setsize(150, 40); apeopletxt=new jtextfield(); apeopletxt.setlocation(200, 150); apeopletxt.setsize(250, 30); aprincipalla=new jlabel(); aprincipalla.settext("请输入项目负责人"); aprincipalla.setlocation(30, 200); aprincipalla.setsize(150, 40); aprincipaltxt=new jtextfield(); aprincipaltxt.setlocation(200, 200); aprincipaltxt.setsize(250, 30); atimestartla=new jlabel(); atimestartla.settext("请输入项目开始时间"); atimestartla.setlocation(30, 250); atimestartla.setsize(150, 40); atimestarttxt=new jtextfield(); atimestarttxt.setlocation(200, 250); atimestarttxt.setsize(250, 30); atimeendla=new jlabel(); atimeendla.settext("请输入项目结束时间"); atimeendla.setlocation(30, 300); atimeendla.setsize(150, 40); atimeendtxt=new jtextfield(); atimeendtxt.setlocation(200, 300); atimeendtxt.setsize(250, 30); addbt=new jbutton(); addbt.settext("新增"); addbt.addactionlistener(this); //addbt.addmouselistener(this); addbt.setlocation(250, 340); addbt.setsize(60, 30); uppa.add(addbt); uppa.add(anumla); uppa.add(anamela); uppa.add(apeoplela); uppa.add(aprincipalla); uppa.add(atimestartla); uppa.add(atimeendla); uppa.add(anametxt); uppa.add(anumtxt); uppa.add(apeopletxt); uppa.add(aprincipaltxt); uppa.add(atimestarttxt); uppa.add(atimeendtxt); uppa.validate(); uppa.repaint(); downpa.validate(); downpa.repaint(); } private void initupdownpawhendelete() { //清空上下面板上的控件 uppa.removeall(); downpa.removeall(); //动态的加载上面板的控件 uppa.setlayout(null); numla=new jlabel(); numla.settext("请输入要删除的项目编号"); numla.setlocation(40, 60); numla.setsize(150, 40); dnumtxt=new jtextfield(); dnumtxt.setlocation(200, 60); dnumtxt.setsize(250, 30); dnumbt=new jbutton(); dnumbt.settext("删除"); dnumbt.addactionlistener(this); dnumbt.setlocation(270, 170); dnumbt.setsize(60, 30); uppa.add(numla); uppa.add(dnumtxt); uppa.add(dnumbt); uppa.validate(); uppa.repaint(); downpa.validate(); downpa.repaint(); } private void initupdownpawhenalert() { //清空上下面板上的控件 uppa.removeall(); downpa.removeall(); //动态的加载上面板的控件 uppa.setlayout(null); alnumla=new jlabel(); alnumla.settext("请输入要修改的项目编号"); alnumla.setlocation(40, 60); alnumla.setsize(150, 40); alsebt=new jbutton(); alsebt.settext("提交"); alsebt.addactionlistener(this); alsebt.setlocation(270, 170); alsebt.setsize(60, 30); allnumtxt=new jtextfield(); allnumtxt.setlocation(200, 60); allnumtxt.setsize(200, 30); uppa.add(alnumla); uppa.add(alsebt); uppa.add(allnumtxt); uppa.validate(); uppa.repaint(); albt=new jbutton(); albt.settext("修改"); albt.addactionlistener(this); albt.setlocation(450, 170); albt.setsize(60, 30); /* allnumla=new jlabel(); allnumla.settext("请输入修改后的项目编号"); allnumla.setlocation(30, 00); allnumla.setsize(150, 40); alnumtxt=new jtextfield(); alnumtxt.setlocation(180, 00); alnumtxt.setsize(250, 30);*/ alnamela=new jlabel(); alnamela.settext("请输入修改后项目名称"); alnamela.setlocation(30, 50); alnamela.setsize(150, 40); alnametxt=new jtextfield(); alnametxt.setlocation(180, 50); alnametxt.setsize(250, 30); alpeoplela=new jlabel(); alpeoplela.settext("请重设参与人员"); alpeoplela.setlocation(30, 100); alpeoplela.setsize(150, 40); alpeopletxt=new jtextfield(); alpeopletxt.setlocation(180, 100); alpeopletxt.setsize(250, 30); alprincipalla=new jlabel(); alprincipalla.settext("请重设项目负责人"); alprincipalla.setlocation(30, 150); alprincipalla.setsize(150, 40); alprincipaltxt=new jtextfield(); alprincipaltxt.setlocation(180, 150); alprincipaltxt.setsize(250, 30); altimestartla=new jlabel(); altimestartla.settext("请重设项目开始时间"); altimestartla.setlocation(30, 200); altimestartla.setsize(150, 40); altimestarttxt=new jtextfield(); altimestarttxt.setlocation(180, 200); altimestarttxt.setsize(250, 30); altimeendla=new jlabel(); altimeendla.settext("请重设项目结束时间"); altimeendla.setlocation(30, 250); altimeendla.setsize(150, 40); altimeendtxt=new jtextfield(); altimeendtxt.setlocation(180, 250); altimeendtxt.setsize(250, 30); downpa.add(albt); //downpa.add(allnumla); downpa.add(alnamela); downpa.add(alpeoplela); downpa.add(alprincipalla); downpa.add(altimestartla); downpa.add(altimeendla); downpa.add(alnametxt); //downpa.add(alnumtxt); downpa.add(alpeopletxt); downpa.add(alprincipaltxt); downpa.add(altimestarttxt); downpa.add(altimeendtxt); downpa.setlayout(null); downpa.validate(); downpa.repaint(); } }
3.3 组织sql语句,链接数据库部分
package 科研信息管理系统; import java.sql.*; import java.util.scanner; public class sql { public void addscienceproject(scienceproject scienceproject) { try { class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string constr="jdbc:oracle:thin:@localhost:1521:xe"; connection con=drivermanager.getconnection(constr,"system","1"); stringbuffer sql=new stringbuffer("insert into science values("+scienceproject.getnum()+",'"+scienceproject.getname()+"','"+scienceproject.getpeople()+"','"+scienceproject.getleader()+"','"+scienceproject.gettimestart()+"','"+scienceproject.gettimefinish()+"')"); statement st=con.createstatement(); st.execute(sql.tostring()); st.close(); con.close(); } catch (exception e) { // todo: handle exception system.out.println(e.tostring()); } } public void delscienceproject(scienceproject scienceproject) { try { class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string constr="jdbc:oracle:thin:@localhost:1521:xe"; connection con=drivermanager.getconnection(constr,"system","1"); stringbuffer sql=new stringbuffer("delete from science where num="+scienceproject.getnum()+""); statement st=con.createstatement(); st.execute(sql.tostring()); st.close(); con.close(); } catch (exception e) { // todo: handle exception system.out.println(e.tostring()); } } public void updscienceproject(scienceproject scienceproject) { try { class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string constr="jdbc:oracle:thin:@localhost:1521:xe"; connection con=drivermanager.getconnection(constr,"system","1"); stringbuffer sql =new stringbuffer("update science set name='"+scienceproject.getname()+"',workpeople='"+scienceproject.getpeople()+"',manager='"+scienceproject.getleader()+"',timestart='"+scienceproject.gettimestart()+"',timeend='"+scienceproject.gettimefinish()+"' where num="+scienceproject.getnum()+""); statement st=con.createstatement(); st.execute(sql.tostring()); st.close(); con.close(); } catch (exception e) { // todo: handle exception system.out.println("修改异常"); system.out.println(e.tostring()); } } public scienceproject findbynum(int num) { scienceproject scienceproject=new scienceproject(); try { class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string constr="jdbc:oracle:thin:@localhost:1521:xe"; connection con=drivermanager.getconnection(constr,"system","1"); stringbuffer sql=new stringbuffer("select * from science where num="+num+""); statement st=con.createstatement(); resultset rs=st.executequery(sql.tostring()); while(rs.next()) { scienceproject.setnum(rs.getint(1)); scienceproject.setname(rs.getstring(2)); scienceproject.setpeople(rs.getstring(3)); scienceproject.setleader(rs.getstring(4)); scienceproject.settimestart(rs.getstring(5)); scienceproject.settimefinish(rs.getstring(6)); } st.close(); con.close(); } catch (exception e) { // todo: handle exception system.out.println(e.tostring()); } return scienceproject; } public scienceproject look() { scienceproject scienceproject=new scienceproject(); try { class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string constr="jdbc:oracle:thin:@localhost:1521:xe"; connection con=drivermanager.getconnection(constr,"system","1"); stringbuffer sql=new stringbuffer("select *from science"); statement st=con.createstatement(); resultset rs=st.executequery(sql.tostring()); while(rs.next()) { scienceproject.setnum(rs.getint(1)); scienceproject.setname(rs.getstring(2)); scienceproject.setpeople(rs.getstring(3)); scienceproject.setleader(rs.getstring(4)); scienceproject.settimestart(rs.getstring(5)); scienceproject.settimefinish(rs.getstring(6)); } st.close(); con.close(); } catch (exception ex) { // todo: handle exception system.out.println(ex.tostring()); } return scienceproject; } public static void main(string args[]) { } }
3.4 增删查改对应处理的源码
//处理增加 package 科研信息管理系统; import javax.swing.joptionpane; public class dealadd { public dealadd() {} public void add(scienceproject s) { sql sql=new sql(); if(sql.findbynum(s.getnum()).getnum()!=0) { joptionpane.showmessagedialog(null, "该项目已存在,请重新输入!"); } else { sql.addscienceproject(s); joptionpane.showmessagedialog(null,"增加成功"); } } }
由设计部分可以看到,删除时需要先提交项目编号,所以,需要处理提交事件
//处理提交 package 科研信息管理系统; import javax.swing.joptionpane; public class dealsub { dealsub() {} public void submit(int num) { sql sql =new sql(); if(sql.findbynum(num).getnum()==0) { joptionpane.showmessagedialog(null, "不存在该项目"); } else { joptionpane.showmessagedialog(null, "请在下方填写修改后的项目信息"); } } } //处理删除 package 科研信息管理系统; import javax.swing.joptionpane; public class dealdelete { public dealdelete() {} public void delete(int num) { scienceproject s=new scienceproject(); sql sql=new sql(); if(sql.findbynum(num).getnum()==0) { joptionpane.showmessagedialog(null, "不存在该项目~"); } else { s=sql.findbynum(num); sql.delscienceproject(s); joptionpane.showmessagedialog(null, "删除成功~"); } } } //处理修改 package 科研信息管理系统; import javax.swing.joptionpane; public class dealalter { public dealalter() {} public void alter(scienceproject s) { sql sql=new sql(); sql.updscienceproject(s); joptionpane.showmessagedialog(null,"修改成功"); } } //处理查询 package 科研信息管理系统; public class dealsearch //处理查询事件 { public dealsearch() {} public string findprobynum(int num)//通过项目编号查询 { string result=""; scienceproject s=new scienceproject(); sql sql=new sql(); s=sql.findbynum(num); result=s.getnum()+","+s.getname()+","+s.getpeople()+","+s.getleader()+","+s.gettimestart()+","+s.gettimefinish(); return result; } }
3.5 主函数调用登录窗口
package 科研信息管理系统; public class test { public static void main(string[] args) { loginwindows v=new loginwindows(); } }
四、总结
还是有一些的缺陷存在的,由于对科研项目的不熟悉,有些属性设计得不合理,还有很多没有考虑到的地方,另外对gui的不熟悉也限制了我们在窗口上的设计。还需要在项目属性及窗口等方面进行修改。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。