学生选课系统连接Mysql数据库.docx
《学生选课系统连接Mysql数据库.docx》由会员分享,可在线阅读,更多相关《学生选课系统连接Mysql数据库.docx(23页珍藏版)》请在冰豆网上搜索。
![学生选课系统连接Mysql数据库.docx](https://file1.bdocx.com/fileroot1/2023-1/30/da43e7ed-fece-4c28-96ff-9cdd019573d7/da43e7ed-fece-4c28-96ff-9cdd019573d71.gif)
学生选课系统连接Mysql数据库
学生选课系统(连接Mysql数据库)
/*
JDBC连接数据库
*/
packageimmoc4.bao;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassJDBC{
privatestaticfinalStringURL="jdbc:
mysql:
//localhost:
3306/a?
characterEncoding=utf8";
privatestaticfinalStringUSER="root";
privatestaticfinalStringPASSWORD="995923";
privatestaticConnectionconn=null;
static{
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}
}
publicstaticConnectiongetConnection(){
returnconn;
}
}
/*
登录界面
*/
packageimmoc4.bao;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.SQLException;
importjava.util.Scanner;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
publicclassViewextendsJFrame{
staticGongNenggn=newGongNeng();
staticStudents=newStudent();
staticJTextFieldtext1;
staticJTextFieldtext2;
staticJButtonbutton;
publicvoiddenglu(){
setLayout(newFlowLayout());
add(newJLabel("用户id:
"));
text1=newJTextField(20);
add(text1);
add(newJLabel("密码:
"));
text2=newJTextField(20);
add(text2);
button=newJButton("登录");
add(button);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
}
publicstaticvoidmain(String[]args){
Viewdl=newView();
dl.setBounds(300,300,300,200);
dl.setTitle("学生选课管理系统");
dl.denglu();
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
dl.dispose();
s.setStudent_id(Integer.parseInt(text1.getText()));
s.setStudent_code(text2.getText());
try{
gn.correspond(s);
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
}
}
/*
主界面
*/
packageimmoc4.bao;
importjavax.swing.*;
importimmoc4.bao.Course;
importjava.awt.*;
importjava.awt.event.*;
importjava.sql.SQLException;
publicclassCardextendsJFrame{
/*
*JFrame的布局管理器是BorderLayout
*/
Courseg=newCourse();
GongNenggn=newGongNeng();
JPanelp;//位于中心区域的面板
JButtonb1,b2,b3,b4;//位于北部区域的四个按钮
JButtonadd;
JButtondelete;
JButtonupdate;
JButtonquery;
JLabeladd_id,add_name,add_teacher_name;
JLabeldelete_id;
JLabelupdate_id,update_name,update_teacher_name;
JLabelquery_id;
JLabelquery_show;
JTextFieldtext_add_id,text_add_name,text_add_teacher_name;
JTextFieldtext_delete_id;
JTextFieldtext_update_id,text_update_name,text_update_teacher_name;
JTextFieldtext_query_id;
JTextAreaarea_query_show;
CardLayoutc;//设置面板p的布局
Card()
{
super();
this.setVisible(true);
this.setBounds(400,300,400,300);
p=newJPanel();//实例化p
c=newCardLayout();//实例化c
p.setLayout(c);//设置面板p的布局为c,等价于p=newJPanel(c);
//实例化4个按钮和标签
b1=newJButton("添加课程");
b2=newJButton("删除课程");
b3=newJButton("修改课程");
b4=newJButton("查询课程");
//实例化4个子面板并设定相应背景颜色
JPanelp1=newJPanel();
JPanelp2=newJPanel();
JPanelp3=newJPanel();
JPanelp4=newJPanel();
//p1.setLayout(
p1.setBackground(Color.green);
p2.setBackground(Color.pink);
p3.setBackground(Color.orange);
p4.setBackground(Color.lightGray);
//把上面4个面板添加到中心面板p中,并把4个面板分别用编号为1,2,3,4代替
p.add(p1,"1");
p.add(p2,"2");
p.add(p3,"3");
p.add(p4,"4");
//实例化位于南部区域的面板,并添加4个按钮
JPanelpnorth=newJPanel();
pnorth.add(b1);
pnorth.add(b2);
pnorth.add(b3);
pnorth.add(b4);
//把p,psourth分别添加到本JFrame的中心区域和南部区域
this.add(p);//等价于this.add(p,BorderLayout.CENTER);因为默认是把组件添加到中部
this.add(pnorth,BorderLayout.NORTH);
//添加课程
add_id=newJLabel("课程号");
text_add_id=newJTextField(30);
add_name=newJLabel("课程名");
text_add_name=newJTextField(30);
add_teacher_name=newJLabel("教师名");
text_add_teacher_name=newJTextField(30);
add=newJButton("添加");
//为按钮b1添加监听器,当被按下时显示面板p中的前一个子面板
b1.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
c.show(p,"1");//显示p中的上一个面板
p1.add(add_id);
p1.add(text_add_id);
p1.add(add_name);
p1.add(text_add_name);
p1.add(add_teacher_name);
p1.add(text_add_teacher_name);
p1.add(add);
setVisible(true);
}
});
add.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
g.setId(Integer.parseInt(text_add_id.getText()));
g.setName(text_add_name.getText());
g.setTeacher_name(text_add_teacher_name.getText());
try{
gn.addcourse(g);
}catch(Exceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
//删除课程
delete_id=newJLabel("课程号");
text_delete_id=newJTextField(20);
delete=newJButton("删除");
//为按钮b2添加监听器,当被按下时显示面板p中的第2个子面板
b2.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
c.show(p,"2");//显示p中代号为2的面板
p2.add(delete_id);
p2.add(text_delete_id);
p2.add(delete);
setVisible(true);
}
});
delete.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
intid=Integer.parseInt(text_delete_id.getText());
try{
gn.delCourse(id);
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
//修改课程
update_id=newJLabel("课程号");
text_update_id=newJTextField(30);
update_name=newJLabel("课程名");
text_update_name=newJTextField(30);
update_teacher_name=newJLabel("教师名");
text_update_teacher_name=newJTextField(30);
update=newJButton("修改");
//以下类推
b3.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
c.show(p,"3");
p3.add(update_id);
p3.add(text_update_id);
p3.add(update_name);
p3.add(text_update_name);
p3.add(update_teacher_name);
p3.add(text_update_teacher_name);
p3.add(update);
setVisible(true);
}
});
update.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
intid=Integer.parseInt(text_update_id.getText());
g.setId(Integer.parseInt(text_update_id.getText()));
g.setName(text_update_name.getText());
g.setTeacher_name(text_update_teacher_name.getText());
try{
gn.updatecourse(g);
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
//查询课程
query_id=newJLabel("课程号");
text_query_id=newJTextField(20);
query=newJButton("查询");
b4.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
c.show(p,"4");
p4.add(query_id);
p4.add(text_query_id);
p4.add(query);
setVisible(true);
}
});
query.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
intid=Integer.parseInt(text_query_id.getText());
try{
gn.get(id);
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
/*
底层增删改查功能
*/
packageimmoc4.bao;
importjava.awt.FlowLayout;
importjava.io.ByteArrayInputStream;
importjava.sql.Connection;
importjava.sql.Date;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
publicclassGongNeng{
Cuecue=newCue();
//添加课程功能
publicvoidaddcourse(Courseg)throwsException{
Listresult=newArrayList();
Connectionconn=JDBC.getConnection();
StringBuildersb=newStringBuilder();
sb.append("select*fromstudent_course");
PreparedStatementptmt=conn.prepareStatement(sb.toString());
ResultSetrs=ptmt.executeQuery();//ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成,用while循环来表示结果集
intx=1;
while(rs.next()){
if(g.getId()==rs.getInt("id")){//当输入的课程号不存在的异常处理
x=0;
}
}
if(x==1){
ptmt=conn.prepareStatement("insertintostudent_course(id,name,teacher_name)values(?
?
?
)");
//SPreparedStatement是SQL语句被预编译并存储在PreparedStatement
//对象中。
然后可以使用此对象多次高效地执行该语句
ptmt.setInt(1,g.getId());
ptmt.setString(2,g.getName());
ptmt.setString(3,g.getTeacher_name());
ptmt.execute();
}else{
cue.denglu("对不起,你输入的课程号已存在,请重新输入");
}
}
//修改课程功能
publicvoidupdatecourse(Courseg)throwsSQLException{
Listresult=newArrayList();
Connectionconn=JDBC.getConnection();
StringBuildersb=newStringBuilder();
sb.append("select*fromstudent_course");
PreparedStatementptmt=conn.prepareStatement(sb.toString());
ResultSetrs=ptmt.executeQuery();//ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
//用while循环来表示结果集
intx=0;
while(rs.next()){
if(g.getId()==rs.getInt("id")){//当输入的课程号不存在的异常处理
x=1;
}
}
if(x==1){
ptmt=conn
.prepareStatement("updatestudent_coursesetname=?
teacher_name=?
whereid=?
");
ptmt.setString(