Java语言课程设计.docx
- 文档编号:12542097
- 上传时间:2023-04-20
- 格式:DOCX
- 页数:26
- 大小:174.89KB
Java语言课程设计.docx
《Java语言课程设计.docx》由会员分享,可在线阅读,更多相关《Java语言课程设计.docx(26页珍藏版)》请在冰豆网上搜索。
Java语言课程设计
2、Java语言课程设计报告参考格式如下:
1、设计内容
自学Swing图形界面设计和数据库程序设计。
开发用于某超市的超市管理系统。
该系统要求实现以下功能模块:
商品信息管理、采购管理、销售管理、用户管理、权限管理。
在系统的开发过程充分考虑系统的方便性、实用性、安全性和规范性
2、设计方案
3、实现方法
我们采用swing图形界面设计以及mysql数据库,数据库连接使用java对mysql的jdbc驱动。
四、主要代码
连接数据库:
publicstaticfinalStringDBURL="jdbc:
mysql:
//localhost:
3306/market";
//MySQL数据库的连接用户名
publicstaticfinalStringDBUSER="root";
//MySQL数据库的连接密码
publicstaticfinalStringDBPASS="123456";
Connectioncon;
publicConnectionopenDB(){
try{
con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
returncon;
}catch(Exceptione)
{
e.printStackTrace();
}
returnnull;
}
登陆实现:
privatevoidloginMouseClicked(java.awt.event.MouseEventevt){
intpower=0;
try{
Connectioncon=openDB();
Statementstmt=con.createStatement();
ResultSetrs=stmt.executeQuery("SELECT*FROMuserinfo");
intpianli=0;
if(user.getText()==null){
JOptionPane.showMessageDialog(this,"请输入用户名!
");
}
elseif(password.getPassword()==null){
JOptionPane.showMessageDialog(this,"请输入密码!
");
}
else{
Stringuserl=user.getText();
Stringpasswordl=String.valueOf(password.getPassword());
while(rs.next())
{
if(userl.equals(rs.getString("user")))
{
cook.user=userl;
if(passwordl.equals(rs.getString("password"))){
if(manager.isSelected()){
power=1;
cook.power=1;
if(rs.getInt("power")==power){
ControllerManager.mloginJFramecontroller.show();
this.dispose();
pianli=1;
}
else
{
JOptionPane.showMessageDialog(this,"用户名或密码有错,请核对后登录");
}
}
elseif(seller.isSelected()){
power=2;
cook.power=2;
if(rs.getInt("power")==power){
newsell().setVisible(true);
this.dispose();
pianli=1;
}
else
{
JOptionPane.showMessageDialog(this,"用户名或密码有错,请核对后登录");
}
}
else{
break;
}
}
}
}
if(pianli==0){
JOptionPane.showMessageDialog(this,"用户名或密码有错,请核对后登录");
}
}
}catch(Exceptione)
{
e.printStackTrace();
}
}
信息修改:
publicinfoChange(){
StringDBsex="";
StringDBbirthday="";
StringDBjob="";
StringDBid="";
StringDBpersonnelId="";
try{
initComponents();
Connectioncon=openDB();
Statementstmt=con.createStatement();
ResultSetrs=stmt.executeQuery("SELECT*FROMuserinfo");
while(rs.next()){
if(rs.getString("user").equals(cook.user)){
DBsex=rs.getString("sex");
DBbirthday=rs.getString("birthday");
DBjob=rs.getString("job");
DBid=rs.getString("ID");
DBpersonnelId=rs.getString("personnelId");
}
}
sex.setSelectedItem(DBsex);
ID.setText(DBid);
job.setText(DBjob);
birthday.setText(DBbirthday);
user.setText(cook.user);
personnelId.setText(DBpersonnelId);
if(cook.power==1){
manager.setSelected(true);
}
else{
sell.setSelected(true);
}
}catch(SQLExceptionex){
Logger.getLogger(infoChange.class.getName()).log(Level.SEVERE,null,ex);
}
}
privatevoidOKMouseClicked(java.awt.event.MouseEventevt){
try{
Connectioncon=openDB();
Statementstmt=con.createStatement();
StringSEX=String.valueOf(sex.getSelectedItem());
StringBirthday=birthday.getText();
StringJob=job.getText();
StringId=ID.getText();
Stringsql="SELECT*FROMuserinfoWHEREuser='"+cook.user+"'";
ResultSetrSet=stmt.executeQuery(sql);
if(rSet.next()==false){
JOptionPane.showMessageDialog(this,"修改的信息不存在!
");
}else
{
Stringstr="UPDATEuserinfoSETsex='"+SEX+"',birthday='"+Birthday+"',job='"+Job+"',ID='"+Id+"'WHEREuser='"+cook.user+"'";
stmt.executeUpdate(str);
JOptionPane.showMessageDialog(this,"添加成功!
");
}
}catch(SQLExceptionex){
Logger.getLogger(infoChange.class.getName()).log(Level.SEVERE,null,ex);
}
}
密码修改:
privatevoidjButton1MouseClicked(java.awt.event.MouseEventevt){
try{
Connectioncon=openDB();
Statementstmt=con.createStatement();
ResultSetrs=stmt.executeQuery("SELECT*FROMuserinfo");
while(rs.next()){
if(rs.getString("user").equals(cook.user)){
if(rs.getString("password").equals(String.valueOf(oldPassword.getPassword()))){
if(String.valueOf(newPassword.getPassword()).equals(String.valueOf(newPassword1.getPassword()))){
Stringsql="UPDATEuserinfoSETpassword='"+String.valueOf(newPassword.getPassword())+"'WHEREuser='"+cook.user+"'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"密码修改成功,请牢记您的密码!
");
}
else{
JOptionPane.showMessageDialog(this,"两次输入的新密码不相同!
");
}
}
else{
JOptionPane.showMessageDialog(this,"原密码输入错误!
");
}
}
}
}catch(SQLExceptionex){
Logger.getLogger(Password.class.getName()).log(Level.SEVERE,null,ex);
}
}
人员管理:
privatevoidjButton1MouseClicked(java.awt.event.MouseEventevt){
DefaultTableModeltableModel=(DefaultTableModel)myTable.getModel();
if(myTable.getSelectedRow()!
=-1){
try{
introw1=myTable.getSelectedRow();
StringcellValue=(String)tableModel.getValueAt(row1,0);
Connectioncon=openDB();
Statementstmt=con.createStatement();
Stringsql="DELETEFROMuserinfoWHEREuser='"+cellValue+"'";
stmt.executeUpdate(sql);
tableModel.removeRow(row1);
JOptionPane.showMessageDialog(this,"用户名:
"+cellValue+"已从数据库删除!
");
}catch(SQLExceptionex){
Logger.getLogger(management.class.getName()).log(Level.SEVERE,null,ex);
}
}
else{
JOptionPane.showMessageDialog(this,"请选中要删除的信息再点击删除!
");
}
}
用户注册:
privatevoidokMouseClicked(java.awt.event.MouseEventevt){
try{
Connectioncon=openDB();
Statementstmt=con.createStatement();
Statementstmt1=con.createStatement();
ResultSetrs=stmt.executeQuery("SELECT*FROMuserinfo");
StringUser=user.getText();
StringPassword=String.valueOf(password.getPassword());
StringPassword1=String.valueOf(password1.getPassword());
StringpersonneID=String.valueOf(personnelId.getText());
StringSEX=String.valueOf(sex.getSelectedItem());
StringBirthday=birthday.getText();
StringJob=job.getText();
StringId=ID.getText();
intpower=0;
if(jRadioButton1.isSelected()){
power=1;
}
elseif(sell.isSelected()){
power=2;
}
if(Password.equals(Password1))
{
intsign=0;
while(rs.next())
{
if(User.equals(rs.getString("user")))
{
sign=1;
JOptionPane.showMessageDialog(this,"用户名已存在!
");
}
}
if(sign==0)
{
Stringstr="INSERTINTOuserinfoVALUES('"+User+"','"+Password+"','"+power+"','"+personneID+"','"+SEX+"','"+Birthday+"','"+Job+"','"+Id+"')";
stmt1.executeUpdate(str);
JOptionPane.showMessageDialog(this,"用户名创建成功!
");
}
}
else{
JOptionPane.showMessageDialog(this,"两次输入的密码不同!
");
}
}catch(Exceptione)
{
e.printStackTrace();
}//TODOaddyourhandlingcodehere:
}
销售管理:
voidacountnumber(){
DefaultTableModeltableModel=(DefaultTableModel)myTable.getModel();
introws=myTable.getRowCount();
floatacount=0;
floatprice=0;
floatquantity=0;
for(inti=0;i price=Float.parseFloat(String.valueOf(tableModel.getValueAt(i,6))); if(String.valueOf(tableModel.getValueAt(i,8)).equals("")){ quantity=0; } else{ quantity=Float.parseFloat(String.valueOf(tableModel.getValueAt(i,8))); } acount=acount+price*quantity; } all.setText(String.valueOf(acount)); } intchose(){ //判断商品是否已加入或者数量大于库存量 inty=0; floatquantity=0; floataddquantity=0; if(Quantity.getText().equals("")){ addquantity=0; } else{ addquantity=Float.parseFloat(Quantity.getText()); } Jnumber.getText(); DefaultTableModeltableModel=(DefaultTableModel)myTable.getModel(); introws=myTable.getRowCount(); try{ Connectioncon=openDB(); Statementstmt=con.createStatement(); ResultSetrs=stmt.executeQuery("SELECT*FROMcommodity"); while(rs.next()){ if(rs.getString("number").equals(Jnumber.getText())){ if(rs.getFloat("fullNumber") y=1; JOptionPane.showMessageDialog(this,"对不起,此商品库存仅剩"+rs.getFloat("fullNumber")); returny; } } } }catch(SQLExceptionex){ Logger.getLogger(MSalesJFrame.class.getName()).log(Level.SEVERE,null,ex); } for(inti=0;i if(String.valueOf(tableModel.getValueAt(i,0)).equals(Jnumber.getText())){ y=1; if(String.valueOf(tableModel.getValueAt(i,8)).equals("")){ quantity=0; } else{ quantity=Float.parseFloat(String.valueOf(tableModel.getValueAt(i,8))); } tableModel.setValueAt(quantity+addquantity,i,8); } } returny; } privatevoidTianjiashangpinMouseClicked(java.awt.event.MouseEventevt){ if(chose()==0){ try{ Connectioncon=openDB(); Statementstmt=con.createStatement(); Statementstmt1=con.createStatement(); ResultSetrs=stmt.executeQuery("SELECT*FROMcommodity"); ResultSetrs1=stmt1.executeQuery("SELECT*FROMcommodity"); intjishu=0; rs1.last(); intsize2=rs1.getRow(); while(rs.next()){ jishu++; if(rs.getString("number").equals(Jnumber.getText())){ myTable.setModel(myTModel); Vectorrec_vector=newVector(); rec_vector.addElement(rs.getString("number")); rec_vector.addElement(rs.getString("CName")); rec_vector.addElement(rs.getString("supplier")); rec_vector.addElement(rs.getString("PArea")); rec_vector.addElement(rs.getString("unit")); rec_vector.addElement(rs.getString("shelfFife")); rec_vector.addElement(rs.getFloat("price")); rec_vector.addElement(rs.getString("pack")); rec_vector.addElement(Quantity.getText()); myTModel.addRow(rec_vector); break; } else{ if(jishu>=size2){ JOptionPane.showMessageDialog(this,"对不起,暂时没有这件商品,请联系管理员添加! "); } } } }catch(SQLExceptionex){ Logger.getLogger(MSalesJFrame.class.getName()).log(Level.SEVERE,null,ex); } } acountnumber(); } privatevoidDeletejButton2MouseClicked(java.awt.event.MouseEventevt){ DefaultTableModeltableModel=(DefaultTableModel)myTable.getModel(); if(myTable.getSelectedRow()! =-1){ introw1=myTable.getSelectedRow(); tableModel.removeRow(row1); } else{ JOptionPane.showMessageDialog(this
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言 课程设计
![提示](https://static.bdocx.com/images/bang_tan.gif)