课程设计作业
在网上代码的基础上做了一些改动(增加为两个菜单:分别为学生信息管理和学生成绩管理)
原网上代码:点击打开链接
java程序连接操作SQLServer详解:点击打开链接
下载jdbc驱动:点击打开链接
jdk环境变量配置:点击打开链接
软件的安装就自己百度教程就好。
1.login表:学生登录账号和密码管理
2.stu表:学生信息表
3.grade表:学生成绩表
package StudentManagementSystem;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class LLogin {
public static void main(String[] args) {
new loginFrame();
}
}
class loginFrame extends JFrame implements ActionListener{
Box box1,box2,baseBox;
JLabel userName,userPwd,tubiao;
JTextField nameField;
JPasswordField pwdField;
JButton button;
JTabbedPane choose;
JPanel panel1,panel2;
loginFrame(){
setBackground(Color.orange);
tubiao=new JLabel(new ImageIcon("图片路径")); //图片在原有基础上要调整大小
add(tubiao,BorderLayout.NORTH);
userName=new JLabel("账号",JLabel.CENTER);
userPwd=new JLabel("密码",JLabel.CENTER);
nameField=new JTextField(8);
pwdField=new JPasswordField(8);
panel1=new JPanel();
panel2=new JPanel();
choose=new JTabbedPane();
choose.add("登录界面",panel1);
panel1.setLayout(new GridLayout(2,2));
panel1.add(userName);panel1.add(nameField);
panel1.add(userPwd);panel1.add(pwdField);
add(choose,BorderLayout.CENTER);
button=new JButton("登陆");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
//小图标
ImageIcon tubiao=new ImageIcon("图片路径");
setIconImage(tubiao.getImage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400,150,550,400);
setTitle("陈可的学生信息管理系统");
validate();
}
public void actionPerformed(ActionEvent e){
String name,pwd;
name=nameField.getText();
pwd=pwdField.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库的密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
rs=sql.executeQuery("select * from login where name ='"+name+"' and pwd='"+pwd+"'");//对应自己数据库建的表填写
int q=0;
while(rs.next()){
q++;
}
if(q>0){
JOptionPane.showMessageDialog(this, "登陆成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
this.dispose();
new CommFrame();
}
else
JOptionPane.showMessageDialog(this, "账号或者密码错误!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
效果:
登陆成功后 点击确定 登陆界面会消失 出现学生信息管理系统管理界面 。
你可以根据要求自己添加工具菜单栏。
package StudentManagementSystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CommFrame extends JFrame implements ActionListener {
JMenuBar bar;
JMenu menu,menu1;
JMenuItem scanItem,deleteItem,updateItem,insertItem,searchItem;
JMenuItem scanItem1,deleteItem1,updateItem1,insertItem1,searchItem1;
Scan_stu scan; //查看所有学生信息
Delete_stu delete;//删除学生信息
Update_stu update;//更新学生信息
Insert_stu insert;//插入学生信息
Search_stu search;//查找学生信息
Scan_grade scan_grade; //查看所有学生成绩
Delete_grade delete_grade;//删除学生成绩
Update_grade update_grade;//更新学生成绩
Insert_grade insert_grade;//插入学生成绩
Search_grade search_grade;//查找学生成绩
CardLayout card=null;
JPanel pCenter;
CommFrame(){
setLayout(new FlowLayout());
scanItem=new JMenuItem("浏览");
deleteItem=new JMenuItem("删除");
updateItem=new JMenuItem("修改");
insertItem=new JMenuItem("添加");
searchItem=new JMenuItem("查找");
scanItem1=new JMenuItem("浏览");
deleteItem1=new JMenuItem("删除");
updateItem1=new JMenuItem("修改");
insertItem1=new JMenuItem("添加");
searchItem1=new JMenuItem("查找");
bar=new JMenuBar();
menu=new JMenu("信息管理菜单");
menu1=new JMenu("成绩管理菜单");
menu.add(scanItem);
menu.add(deleteItem);
menu.add(updateItem);
menu.add(insertItem);
menu.add(searchItem);
menu1.add(scanItem1);
menu1.add(deleteItem1);
menu1.add(updateItem1);
menu1.add(insertItem1);
menu1.add(searchItem1);
bar.add(menu);
bar.add(menu1);
setJMenuBar(bar);
scanItem.addActionListener(this);
deleteItem.addActionListener(this);
updateItem.addActionListener(this);
insertItem.addActionListener(this);
searchItem.addActionListener(this);
scanItem1.addActionListener(this);
deleteItem1.addActionListener(this);
updateItem1.addActionListener(this);
insertItem1.addActionListener(this);
searchItem1.addActionListener(this);
scan=new Scan_stu();
update=new Update_stu();
delete=new Delete_stu();
insert=new Insert_stu();
search=new Search_stu();
card=new CardLayout();
scan_grade=new Scan_grade();
update_grade=new Update_grade();
delete_grade=new Delete_grade();
insert_grade=new Insert_grade();
search_grade=new Search_grade();
card=new CardLayout();
pCenter=new JPanel();
pCenter.setLayout(card);
pCenter.add("scanItem", scan);
pCenter.add("deleteItem",delete);
pCenter.add("updateItem",update);
pCenter.add("insertItem",insert);
pCenter.add("searchItem",search);
pCenter.add("scanItem1", scan_grade);
pCenter.add("deleteItem1",delete_grade);
pCenter.add("updateItem1",update_grade);
pCenter.add("insertItem1",insert_grade);
pCenter.add("searchItem1",search_grade);
add(pCenter,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400,150,550,400);
setTitle("学生信息管理系统");
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==scanItem)
card.show(pCenter, "scanItem");
else if(e.getSource()==deleteItem)
card.show(pCenter, "deleteItem");
else if(e.getSource()==updateItem)
card.show(pCenter, "updateItem");
else if(e.getSource()==insertItem)
card.show(pCenter, "insertItem");
else if(e.getSource()==searchItem)
card.show(pCenter, "searchItem");
if(e.getSource()==scanItem1)
card.show(pCenter, "scanItem1");
else if(e.getSource()==deleteItem1)
card.show(pCenter, "deleteItem1");
else if(e.getSource()==updateItem1)
card.show(pCenter, "updateItem1");
else if(e.getSource()==insertItem1)
card.show(pCenter, "insertItem1");
else if(e.getSource()==searchItem1)
card.show(pCenter, "searchItem1");
}
}
效果:
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Scan_stu extends JPanel implements ActionListener{
DefaultTableModel update_table;
JTable table;
Query query;
JButton button;
Object a[][];
String b[];
Scan_stu(){
setLayout(new FlowLayout());
setBackground(Color.orange);
query=new Query();
query.setTableName("stu");
a=query.getRecord();
b=query.getField();
update_table=new DefaultTableModel(a, b);
table=new JTable(update_table);
button=new JButton("更新");
button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出现滑动条
add(scrollPane);
add(button);
}
public void actionPerformed(ActionEvent e){
a=null;b=null;
query=new Query();
query.setTableName("stu");
a=query.getRecord();
b=query.getField();
update_table.setDataVector(a, b);
}
}
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Scan_grade extends JPanel implements ActionListener{
DefaultTableModel update_table;
JTable table;
Query query;
JButton button;
Object a[][];
String b[];
Scan_grade(){
setLayout(new FlowLayout());
setBackground(Color.orange);
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table=new DefaultTableModel(a, b);
table=new JTable(update_table);
button=new JButton("更新");
button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出现滑动条
add(scrollPane);
add(button);
}
public void actionPerformed(ActionEvent e){
a=null;b=null;
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table.setDataVector(a, b);
}
}
package StudentManagementSystem;
import java.sql.*;
public class Query {
Object a[][]=null;
String b[]=null;
String tableName="";
int 字段个数;
public Query(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException e){
System.out.println(e);
}
}
public Object[][] getRecord(){//求表格的内容
a=null;
b=null;
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="chenke010919chan";
con=DriverManager.getConnection(url,userName,userPwd);
int 字段个数=getZiDuan();
int n=getAmount();
a=new Object[n][字段个数];
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
int m=0;
while(rs.next()){
for(int k=1;k<=字段个数;k++){
a[m][k-1]=rs.getString(k);
}
System.out.println();
m++;
}
con.close();
}
catch(SQLException e){
System.out.println("请输入正确的表名"+e);
}
return a;
}
public int getAmount(){//求表内容有多少行
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
rs.last();
int rows=rs.getRow();
return rows;
}
catch(SQLException exp){
System.out.println(""+exp);
return 0;
}
}
public String[] getField(){//求字段名称
Connection con;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
int 字段个数=getZiDuan();
b=new String[字段个数];
int k=0;
while(rs1.next()){
b[k]=rs1.getString(4);
k++;
}
con.close();
}
catch(SQLException e){
System.out.println(e);
}
return b;
}
public void setTableName(String s){//初试表名
tableName=s.trim();
}
public int getZiDuan(){//求字段个数
Connection con;
PreparedStatement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
字段个数=0;
while(rs1.next())
字段个数++;
}
catch(SQLException e){
System.out.println(e);
}
return 字段个数;
}
}
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Delete extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Delete(){
}
public void Execute_Delete(int n){
String SQL="";
Connection con;
Statement sql;
SQL="delete from "+tableName+" where "+a[n]+" ='"+field[n].getText().toString()+"'";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.executeUpdate(SQL);
con.close();
JOptionPane.showMessageDialog(this, "删除成功","消息对话框",JOptionPane.WARNING_MESSAGE);
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Update extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Update(){
}
public void Execute_Update(int mark){
String SQL[]=new String [a.length];
Connection con;
Statement sql;
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert;
String tableName;
Insert_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert1;
String tableName;
Insert_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Insert extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Update update;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Insert(){
}
public void Execute_Insert(int mark){
String SQL;
Connection con;
Statement sql;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
SQL="insert "+tableName+"("+a[mark]+") values ('"+field[mark].getText().toString()+"')";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.execute(SQL);
con.close();
update =new Update();
update.setTableName(tableName);
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "添加成功!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_stu extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_stu(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_grade extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_grade(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Search extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Object object[][];
String b[];
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Search(){
}
public void Execute_Search(){
String SQL="select * from "+tableName+" where ";
Connection con;
Statement sql;
ResultSet rs;
int sum=0;
for(int i=0;i