java可视化界面操作数据库编程Word文档格式.docx
《java可视化界面操作数据库编程Word文档格式.docx》由会员分享,可在线阅读,更多相关《java可视化界面操作数据库编程Word文档格式.docx(30页珍藏版)》请在冰点文库上搜索。
系统名称:
学生信息管理系统
操作系统:
Window8.1
Java环境;
、jre1.7.0_21
集成开发环境:
MyEclipseProfessional2013
数据库软件版本:
SQLServer2010连接驱动:
jdbc-odbc
二、系统主要功能
本项目主要实现了java界面的登录功能,以及登陆后的增删改查功能,主要是对student表实现增加一条学生信息、删除一条学生信息记录、更新学生信息表,以及查询学生信息表的数据
三、数据库结构
1.数据库名称:
SchoolDB
2.数据表:
student表具体字段信息如下表:
字段名
类型
长度
小数点
不是null
sname(主键)
varchar
20
√
sname
sdept
sgrade
saddress
sname,sname,sdept,sgrade、saddress分别保存学生的姓名、专业、学院、成绩、家庭住址等学生基本信息,其中sname为主键且不为空。
四、关键功能界面截图
1.登陆界面
用户输入数据库中存在的用户名和密码后,便可登陆成功,否则提示密码错误
1正确的用户名,错误的密码
正确的用户名正确的密码后进入学生信息管理系统:
2.添加功能:
输入界面中的所有数据之后,点击添加,即可向数据库中加入一条学生记录
3.删除功能:
输入姓名,即可把数据库中对应姓名的一条学生信息删除
4.更新功能:
输入要修改学生的信息,即可修改该学生在数据库中的记录
5.查找功能:
点击查找可以查询数据库中的所有学生信息,每页9条记录,其余的学生信息的点击下一页,可以查看,还可以点击上一页,来切换数据
点击下一页:
五、关键代码分析
DBConnetion用于连接数据库:
packageedu;
publicclassDBConnection{
publicstaticConnectiongetConnection(){
Connectionconn=null;
Stringurl="
jdbc:
odbc:
schoolData"
;
Stringusername="
sa"
Stringpassword="
123456"
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
}
returnconn;
}
登陆的窗体类LoginFrame、JFrame:
publicclassJswingimplementsActionListener{
privateLoginFrameframe;
privateJLabellname;
privateJLabellpassword;
privateJTextFieldtname;
privateJPasswordFieldtpassword;
privateJButtonbyes;
privateJButtonbno;
privateJPanelp1;
privateJPanelp2;
privateJPanelp3;
privateStringuserName,password;
/**
*初始化窗口数据
*/
publicvoidinitData(){
frame=newLoginFrame("
登陆"
);
frame.setResizable(false);
frame.setLayout(newGridLayout(3,2));
Containerc=frame.getContentPane();
lname=newJLabel("
用户名:
"
lpassword=newJLabel("
密码:
"
tname=newJTextField(8);
tpassword=newJPasswordField(8);
byes=newJButton("
确定"
bno=newJButton("
取消"
p1=newJPanel();
p2=newJPanel();
p3=newJPanel();
p1.add(lname);
p1.add(tname);
c.add(p1);
p2.add(lpassword);
p2.add(tpassword);
c.add(p2);
p3.add(byes);
p3.add(bno);
c.add(p3);
frame.setVisible(true);
byes.addActionListener(this);
bno.addActionListener(this);
userName=null;
password=null;
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==bno){
System.exit
(1);
}else{
if(e.getSource()==byes){
userName=tname.getText().trim();
password=newString(tpassword.getPassword());
booleanresult=false;
result=SchoolOperate.login(userName,password);
if(result){
frame.setVisible(false);
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
SchoolInfoGuiapplication=newSchoolInfoGui();
application.getJFrame().setVisible(true);
}
});
}else{
Stringerror=null;
if(result==false){
error="
用户名或密码错误,请重试!
}
JOptionPane.showMessageDialog(null,error);
}
}
}
publicstaticvoidmain(String[]args){
newJswing().initData();
publicclassLoginFrameextendsJFrame{
publicLoginFrame(Stringtitle)throwsHeadlessException{
super();
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(540,200,280,200);
学生信息管理系统主界面的SchoolInfoGui:
publicclassSchoolInfoGuiimplementsActionListener{
privateJFramejFrame;
privateJPaneljContentPane;
privateJLabellblName=null;
privateJTextFieldtfName=null;
privateJLabellblRegion=null;
privateJComboBoxcbRegion=null;
privateJLabellblArea=null;
privateJTextFieldtfArea=null;
privateJLabellblPopu=null;
privateJTextFieldtfPopu=null;
privateJLabellblGdp=null;
privateJTextFieldtfGDP=null;
privateJButtonbtnAdd=null;
privateJButtonbtnDel=null;
privateJButtonbtnModify=null;
privateJButtonbtnQuery=null;
privateJButtonbtnUpPage=null;
privateJButtonbtnDownPage=null;
privateJTabletable=null;
TableModeltm=null;
JScrollPanejsp=null;
privateJTextFieldgetTfName(){
if(tfName==null){
tfName=newJTextField();
tfName.setBounds(newRectangle(89,17,173,27));
returntfName;
privateJComboBoxgetCbRegion(){
if(cbRegion==null){
String[]str={"
请选择"
"
计算机科学与技术"
信息安全"
网络工程"
信息与计算科学"
物流工程"
软件工程"
};
cbRegion=newJComboBox(str);
cbRegion.setBounds(newRectangle(360,17,229,28));
returncbRegion;
privateJTextFieldgetTfArea(){
if(tfArea==null){
tfArea=newJTextField();
tfArea.setBounds(newRectangle(89,61,173,27));
returntfArea;
privateJTextFieldgetTfPopu(){
if(tfPopu==null){
tfPopu=newJTextField();
tfPopu.setBounds(newRectangle(360,61,229,28));
returntfPopu;
privateJTextFieldgetTfGDP(){
if(tfGDP==null){
tfGDP=newJTextField();
tfGDP.setBounds(newRectangle(89,105,173,27));
returntfGDP;
privateJButtongetBtnAdd(){
if(btnAdd==null){
btnAdd=newJButton();
btnAdd.setBounds(newRectangle(46,152,71,30));
btnAdd.addActionListener(this);
btnAdd.setText("
添加"
returnbtnAdd;
privateJButtongetBtnUpPage(){
if(btnUpPage==null){
btnUpPage=newJButton("
上一页"
btnUpPage.addActionListener(this);
btnUpPage.setBounds(200,390,80,30);
returnbtnUpPage;
privateJButtongetBtnDownPage(){
if(btnDownPage==null){
btnDownPage=newJButton("
下一页"
btnDownPage.addActionListener(this);
btnDownPage.setBounds(300,390,80,30);
returnbtnDownPage;
privateJButtongetBtnDel(){
if(btnDel==null){
btnDel=newJButton();
btnDel.setBounds(newRectangle(189,152,71,30));
btnDel.addActionListener(this);
btnDel.setText("
删除"
returnbtnDel;
privateJButtongetBtnModify(){
if(btnModify==null){
btnModify=newJButton();
btnModify.setBounds(newRectangle(342,152,71,30));
btnModify.addActionListener(this);
btnModify.setText("
更新"
returnbtnModify;
privateJButtongetBtnQuery(){
if(btnQuery==null){
btnQuery=newJButton();
btnQuery.setBounds(newRectangle(484,152,71,30));
btnQuery.addActionListener(this);
btnQuery.setText("
查找"
returnbtnQuery;
JFramegetJFrame(){
if(jFrame==null){
jFrame=newJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setBounds(newRectangle(450,250,610,454));
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("
学生信息管理系统"
returnjFrame;
privateJPanelgetJContentPane(){
if(jContentPane==null){
lblGdp=newJLabel();
lblGdp.setBounds(newRectangle(15,106,63,27));
lblGdp.setText("
家庭住址"
lblPopu=newJLabel();
lblPopu.setBounds(newRectangle(285,61,63,27));
lblPopu.setText("
成绩"
lblArea=newJLabel();
lblArea.setBounds(newRectangle(15,61,63,27));
lblArea.setText("
学院"
lblRegion=newJLabel();
lblRegion.setBounds(newRectangle(285,17,63,27));
lblRegion.setText("
专业"
lblName=newJLabel();
lblName.setBounds(newRectangle(15,17,63,27));
lblName.setText("
姓名"
jContentPane=newJPanel();
jContentPane.setLayout(null);
jContentPane.add(lblName,null);
jContentPane.add(getTfName(),null);
jContentPane.add(lblRegion,null);
jContentPane.add(getCbRegion(),null);
jContentPane.add(lblArea,null);
jContentPane.add(getTfArea(),null);
jContentPane.add(lblPopu,null);
jContentPane.add(getTfPopu(),null);
jContentPane.add(lblGdp,null);
jContentPane.add(getTfGDP(),null);
jContentPane.add(getBtnAdd(),null);
jContentPane.add(getBtnDel(),null);
jContentPane.add(getBtnModify(),null);
jContentPane.add(getBtnQuery(),null);
jContentPane.add(getBtnUpPage(),null);
jContentPane.add(getBtnDownPage(),null);
returnjContentPane;
if(e.getActionCommand().equals("
)){
selectAll();
}elseif(e.getActionCommand().equals("
selectDownPage();
selectUpPage();
if(tfName.getText().equals("
)
||cbRegion.getSelectedItem().equals("
||tfArea.getText().equals("
||tfPopu.getText().equals("
||tfGDP.getText().equals("
JOptionPane.showMessageDialog(jFrame,"
请填写相关信息"
}else{
Studentco=newStudent();
co.setSname(tfName.getText());
co.setSpro((String)cbRegion.getSelectedItem());
co.setSdept(tfArea.getText());
co.setSgrade(tfPopu.getText());
co.setSaddress(tfGDP.getText());
booleanflag=SchoolOperate.add(co);
if(flag){
添加成功"
}else{
添加失败"
if(!
tfName.getText().equals("
)){
Stringkey=tfName.getText();
booleanflag=SchoolOperate.del(key);
删除成功"
删除失败"
请填写一个国家名字"
if(!
&
&
!
cbRegion.getSelectedItem().equals("
tfArea.getText().equals("
tfPopu.getText().