课程设计目
通次设计做出型理系统加强JAVA学知识巩固融会贯通说学期学知识结加深JAVA数库理解
二需求分析
功需求分析:
该系统具备理学生信息课程信息选课信息功:户通输入账号密码进该系统进行基础维护(学生信息维护课程信息维护选课信息维护)全部进行增加修改删模糊查询
三数项:
1表admin(户表)
Field
Type
Null
Key
Comment
Username
char(10)
——
PRI
户名
password
char(10)
——
——
密码
Name
Char(10)
——
——
户昵称
2表S(学生信息表)
Field
Type
Null
Key
Comment
Sno
nvarchar(50)
——
PRI
学号
Sname
nvarchar(50)
——
——
姓名
Sx
nvarchar(50)
——
——
系
3表C(课程信息表)
Field
Type
Null
Key
Comment
Cno
nvarchar(50)
——
PRI
课号
Cname
nvarchar(50)
——
——
课名
4表SC(学生信息表)
Field
Type
Null
Key
Comment
Cno
nvarchar(50)
——
PRI
课号
Sno
nvarchar(50)
——
PRI
学号
C
nvarchar(50)
——
——
成绩
四系统功描述
该型系统理学生课程基信息功包括四方面:
1理学生信息中包括添加删修改等操作
2理课程信息中包括添加删修改等操作
3理选课信息中包括添加删修改等操作
4查询信息中包括查询学生信息查询课程信息查询选课信息等操作
选课信息
课程信息
学生信息
删信息
修改信息
添加信息
删信息
修改信息
添加信息
删信息
修改信息
添加信息
查询理
选课理
课程理
学生理
学生信息理系统
五代码分析
连接数库方法:
java连接数库驱动关数库操作
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test处student数库名
String userName sa 默认户名
String userPwd mima 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful)
果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()}
return dbConn}
户登录界面userjava:
登录时账号密码未填写输入错误账号密码会提出错误提示框填写账号密码会读取数库里admin表查询输入否存误登录户界面
public class User extends JFrame{
private JLabel usepassword
private JTextField k1户名输入框
private JPasswordField k2密码输入框
private JButton b1b2
登录窗口
public User(JFrame f){
super(系统登录)
Container cgetContentPane()
csetLayout(new FlowLayout())
usenew JLabel(username)
usesetFont(new Font(SerifFontPLAIN20))
passwordnew JLabel(password)
passwordsetFont(new Font(SerifFontPLAIN20))
k1new JTextField(12)
k2new JPasswordField(12)
b1new JButton(登录)
b2new JButton(退出)
设置登录方法
BHandler bnew BHandler()
EXIT dnew EXIT()
b1addActionListener(b)
b2addActionListener(d)
添加控件
cadd(use)
cadd(k1)
cadd(password)
cadd(k2)
cadd(b1)
cadd(b2)
setBounds(600300250150)
setVisible(true)
setResizable(false)
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE)}
函数
public static void main(String[] args) {
User f1new User(new JFrame())}
效果图:
户界面:Menujava
户界面菜单栏4级菜单学生理课程理选课理添加修改删数分操作数库里S表(学生)C表(课程)SC表(选课)查询理进行三表查询
import javaawt*
import javaawtevent*
import javaxswing*
public class Menu extends JFrame implements ActionListener{
Addstu 增加学生界面
Updatastu 修改学生界面
Delstu 删学生界面
AddC 增加课程界面
DelC 删课程界面
UpdateC 修改课程界面
AddSC 增加选课界面
DelSC 删选课界面
UpdateSC 修改选课界面
Selstu 学生查询界面
JPanel pCenter
CardLayout cardnull
JLabel labelnull
JMenuBar mbnew JMenuBar()菜单栏
JMenu m1new JMenu(学生理)
JMenuItem add1new JMenuItem(add1 )
JMenuItem updata1new JMenuItem(updata1 )
JMenuItem delete1new JMenuItem(delete1 )
JMenu m2new JMenu(课程理)
JMenuItem add2new JMenuItem(add2 )
JMenuItem updata2new JMenuItem(updata2 )
JMenuItem delete2new JMenuItem(delete2 )
JMenu m3new JMenu(选课理)
JMenuItem add3new JMenuItem(add3 )
JMenuItem updata3new JMenuItem(updata3 )
JMenuItem delete3new JMenuItem(delete3 )
JMenu m4new JMenu(查询理)
JMenuItem 学生查询new JMenuItem(查询信息 )
JMenuItem m5new JMenuItem(系统退出)
Font tnew Font (sanerifFontPLAIN12)
public Menu (){
thissetTitle(学生选课理系统)
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
组合菜单
addMenu1()
addMenu2()
addMenu3()
addMenu4()
addJMenuBar()
setJMenuBar(mb)
labelnew JLabel(选课理系统JLabelCENTER)
labelsetFont(new Font(宋体FontBOLD36))
labelsetHorizontalTextPosition(SwingConstantsCENTER)
labelsetForeground(Colorred)
点击事件
add1addActionListener(this)
updata1addActionListener(this)
delete1addActionListener(this)
m5addActionListener(this)
add2addActionListener(this)
delete2addActionListener(this)
updata2addActionListener(this)
add3addActionListener(this)
delete3addActionListener(this)
updata3addActionListener(this)
学生查询addActionListener(this)
cardnew CardLayout()
pCenternew JPanel()
pCentersetLayout(card)
增加学生界面new Addstu()
修改学生界面new Updatastu()
删学生界面new Delstu()
增加课程界面new AddC()
删课程界面new DelC()
修改课程界面new UpdateC()
增加选课界面new AddSC()
删选课界面new DelSC()
修改选课界面new UpdateSC()
学生查询界面new Selstu()
pCenteradd(欢迎界面label)
pCenteradd(增加学生界面增加学生界面)
pCenteradd(修改学生界面修改学生界面)
pCenteradd(删学生界面删学生界面)
pCenteradd(增加课程界面增加课程界面)
pCenteradd(删课程界面删课程界面)
pCenteradd(修改课程界面修改课程界面)
pCenteradd(增加选课界面增加选课界面)
pCenteradd(删选课界面删选课界面)
pCenteradd(修改选课界面修改选课界面)
pCenteradd(学生查询界面 学生查询界面)
add(pCenterBorderLayoutCENTER)
validate()
setVisible(true)
setBounds(400150400280)
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
addWindowListener(new WindowAdapter(){关闭程序时操作
public void windowClosing(WindowEvent e){Systemexit(0)} })
validate()}
private void addJMenuBar() {
mbadd(m1)mbadd(m2)mbadd(m3)mbadd(m4)mbadd(m5)}
private void addMenu4() {
m4add(学生查询)m4setFont(t)}
private void addMenu3() {
m3add(add3)m3add(updata3)m3add(delete3)m3setFont(t) }
private void addMenu2() {菜单加入菜单栏中
m2add(add2)m2add(updata2)m2add(delete2)m2setFont(t) }
private void addMenu1() {
m1add(add1)m1add(updata1)m1add(delete1)m1setFont(t) }
public void actionPerformed(ActionEvent e){
Object objegetSource()
if(objm5){Systemexit(0)}
else{if(objadd1){cardshow(pCenter增加学生界面)}
else{if(objupdata1){ cardshow(pCenter修改学生界面)}
else{if(objdelete1){ cardshow(pCenter 删学生界面)}
else{if(objadd2){ cardshow(pCenter 增加课程界面)}
else{if(objdelete2){ cardshow(pCenter 删课程界面)}
else{if(objupdata2){ cardshow(pCenter 修改课程界面)}
else{if(objadd3){ cardshow(pCenter 增加选课界面)}
else{if(objdelete3){ cardshow(pCenter 删选课界面)}
else{if(objupdata3){ cardshow(pCenter 修改选课界面)}
else{if(obj学生查询){ cardshow(pCenter 学生查询界面)
}}}}}}}}}}}}
public static void main(String[] args) {new Menu()}}
效果图:
添加界面AddCjavaAddSCjavaAddstujava
添加数时遇必须信息未填写重复信息数库中已存会提示法添加错误原
import javaawt*
import javaxswing*
import javasql*
import javaawtevent*
public class AddC extends JPanel implements ActionListener{
JTextField 课号课名
JButton 录入
public AddC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}catch(Exception e){Systemerrprintln(设置外观 +e)}
课号new JTextField(12)
课名new JTextField(12)
录入new JButton(录入)
录入addActionListener(this)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
box1add(new JLabel(课号))
box1add(课号)
box2add(new JLabel(课名))
box2add(课名)
box4add(录入)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel messPanelnew JPanel()
messPaneladd(boxH)
setLayout(new BorderLayout())
add(messPanelBorderLayoutCENTER)
validate()}
效果图:
添加界面UpdateCjavaUpdateSCjava Updatestujava
先查找想修改项目修改信息包括号码(学号课号)会进行查询果修改号码已存修改必须信息变成空会提示法修改错误原外修改C表S表会连SC表中应信息起修改
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class UpdateC extends JPanel implements ActionListener{
String savenull
JTextField 课号1课号课名
JButton 修改查找
public UpdateC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}catch(Exception e){Systemerrprintln(设置外观 +e)}
课号1new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
修改new JButton(修改)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(课名JLabelCENTER))
box2add(课名)
box3add(修改)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(查找)
修改addActionListener(this)
查找addActionListener(this)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()}
删界面DelCjavaDelSCjava Deltujava
先查找想删项目然删外删C表S表容会连SC表中应信息起修改
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class DelC extends JPanel implements ActionListener{
String savenull
JTextField 课号1课号课名
JButton 删查找
public DelC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}catch(Exception e){Systemerrprintln(设置外观 +e)}
课号1new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
删new JButton(删)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(课名JLabelCENTER))
box2add(课名)
box4add(删)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(查找)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
删addActionListener(this)
查找addActionListener(this)
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()}
查询界面Selstojava
意条件模糊查找相关表
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class Selstu extends JPanel implements ActionListener{
JTextField 学号姓名系
JButton 查找
JTextField 课号课名
JButton 查找1
JTextField 课号1学号1成绩
JButton 查找2
public Selstu (){
学号new JTextField(12)
姓名new JTextField(12)
系new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
课号1new JTextField(12)
学号1new JTextField(12)
成绩new JTextField(12)
查找new JButton(查找学生信息)
查找1new JButton(查找课程信息)
查找2new JButton(查找选课信息)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
Box box6BoxcreateHorizontalBox()
Box box7BoxcreateHorizontalBox()
box1add(new JLabel(学号JLabelCENTER))
box1add(学号)
box1add(new JLabel(姓名JLabelCENTER))
box1add(姓名)
box1add(new JLabel(系JLabelCENTER))
box1add(系)
box2add(查找)
box4add(new JLabel(课号JLabelCENTER))
box4add(课号)
box4add(new JLabel(课名JLabelCENTER))
box4add(课名)
box6add(查找1)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(new JLabel(成绩JLabelCENTER))
box5add(成绩)
box7add(查找2)
Box boxH1BoxcreateVerticalBox()竖放box
boxH1add(box1)
boxH1add(box2)
boxH1add(BoxcreateVerticalGlue())
Box boxH2BoxcreateVerticalBox()竖放box
boxH2add(box4)
boxH2add(box6)
boxH2add(BoxcreateVerticalGlue())
Box boxH3BoxcreateVerticalBox()竖放box
boxH3add(box5)
boxH3add(box7)
boxH3add(BoxcreateVerticalGlue())
查找addActionListener(this)
查找1addActionListener(this)
查找2addActionListener(this)
JPanel messPanelnew JPanel()
JPanel picPanelnew JPanel()
JPanel threePanelnew JPanel()
messPaneladd(boxH1)
picPaneladd(boxH2)
threePaneladd(boxH3)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
JSplitPane splitV1new JSplitPane(JSplitPaneVERTICAL_SPLITsplitVthreePanel)分割
add(splitV1BorderLayoutCENTER)
validate()}
效果图:
面java源代码总15类分AddCjavaAddSCjavaAddstujava DelstujavaDelCjava DelSCjavaMenujavaSelCjavaSelSCjavaSelstujavaStudentjavaUpdatastujavaUpdateCjavaUpdateSCjavaUserjava
太懒类名错误没修改没优化系统总算运行数库sql2005果数库改驱动具体网百度吧果空优化滴……
AddCjaba:
import javaawt*
import javaxswing*
import javasql*
import javaawtevent*
public class AddC extends JPanel implements ActionListener{
JTextField 课号课名
JButton 录入
public AddC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
课号new JTextField(12)
课名new JTextField(12)
录入new JButton(录入)
录入addActionListener(this)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
box1add(new JLabel(课号))
box1add(课号)
box2add(new JLabel(课名))
box2add(课名)
box4add(录入)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel messPanelnew JPanel()
messPaneladd(boxH)
setLayout(new BorderLayout())
add(messPanelBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent c){
Object objcgetSource()
if(obj录入){
if(课号getText()equals()||课名getText()equals()){
JOptionPaneshowMessageDialog(this学生信息请填满录入 )
}
Statement stmtnull
ResultSet rsnullrs1null
String sqlsql1
sql1select * from C where Cno'+课号getText()+'
sqlinsert into C values('+课号getText()+''+课名getText()+')
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){JOptionPaneshowMessageDialog(this该课号存法添加)}
else{
stmtexecuteUpdate(sql)
JOptionPaneshowMessageDialog(this添加成功)
}
rs1close()
stmtclose()
}
catch(SQLException e){
Systemoutprint(SQL Exception occurMessage is+egetMessage())
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
AddSCjava:
import javaawt*
import javaxswing*
import javasql*
import javautil*
import javaxswingfilechooser*
import javaio*
import javaawtevent*
public class AddSC extends JPanel implements ActionListener{
JTextField 课号学号成绩
JButton 录入
public AddSC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
课号new JTextField(12)
学号new JTextField(12)
成绩new JTextField(12)
录入new JButton(录入)
录入addActionListener(this)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
box1add(new JLabel(课号))
box1add(课号)
box2add(new JLabel(学号))
box2add(学号)
box3add(new JLabel(成绩))
box3add(成绩)
box4add(录入)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel messPanelnew JPanel()
messPaneladd(boxH)
setLayout(new BorderLayout())
add(messPanelBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent c){
Object objcgetSource()
if(obj录入){
if(课号getText()equals()||学号getText()equals()){
JOptionPaneshowMessageDialog(this填写课号学号录入 )
}
else
{
Statement stmtnull
ResultSet rsnullrs1nullrsCnullrsSnull
String sqlsql1sqlSsqlC
sqlCselect * from C where Cno'+课号getText()+'
sqlSselect * from S where Sno'+学号getText()+'
sql1select * from SC where Cno'+课号getText()+' and Sno'+学号getText()+'
sqlinsert into SC values('+课号getText()+''+学号getText()+''+成绩getText()+')
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_R
EAD_ONLY)
rsCstmtexecuteQuery(sqlC)
if(rsCnext()){
rsSstmtexecuteQuery(sqlS)
if(rsSnext()){
rs1stmtexecuteQuery(sql1)
if(rs1next()){JOptionPaneshowMessageDialog(this该学生选该课程号法添加)}
else{
stmtexecuteUpdate(sql)
JOptionPaneshowMessageDialog(this添加成功)
}
rs1close()
}
else{JOptionPaneshowMessageDialog(this该学生存法添加)}
rsSclose()
}
else{JOptionPaneshowMessageDialog(this该课程存法添加)}
rsCclose()
stmtclose()
}
catch(SQLException e){
Systemoutprint(SQL Exception occurMessage is+egetMessage())
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Addstujaba:
import javaawt*
import javaxswing*
import javasql*
import javaawtevent*
public class Addstu extends JPanel implements ActionListener{
JTextField 学号姓名系
JButton 录入
public Addstu(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
学号new JTextField(12)
姓名new JTextField(12)
系new JTextField(12)
录入new JButton(录入)
录入addActionListener(this)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
box1add(new JLabel(学号*JLabelCENTER*))
box1add(学号)
box2add(new JLabel(姓名*JLabelCENTER*))
box2add(姓名)
box3add(new JLabel(系*JLabelCENTER*))
box3add(系)
box4add(录入)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel messPanelnew JPanel()
messPaneladd(boxH)
setLayout(new BorderLayout())
add(messPanelBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent c){
Object objcgetSource()
if(obj录入){
if(学号getText()equals()||姓名getText()equals()||系getText()equals()){
JOptionPaneshowMessageDialog(this学生信息请填满录入 )
}
Statement stmtnull
ResultSet rs1null
String sqlsql1
sql1select * from S where Sno'+学号getText()+'
sqlinsert into S values('+学号getText()+''+姓名getText()+''+系getText()+')
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){JOptionPaneshowMessageDialog(this该学号存法添加)}
else{
stmtexecuteUpdate(sql)
JOptionPaneshowMessageDialog(this添加成功)
}
rs1close()
stmtclose()
}
catch(SQLException e){
Systemoutprint(SQL Exception occurMessage is+egetMessage())
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
DelCjava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class DelC extends JPanel implements ActionListener{
String savenull
JTextField 课号1课号课名
JButton 删查找
public DelC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
课号1new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
删new JButton(删)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(课名JLabelCENTER))
box2add(课名)
box4add(删)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(查找)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
删addActionListener(this)
查找addActionListener(this)
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1null
String sqlnullsql1nullsqlSCnull
if(obj查找){if(课号1getText()equals())JOptionPaneshowMessageDialog(this请填写查询课号 )
else{
sql1select * from C where Cno'+课号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){课号setText(rs1getString(Cno)trim())
课名setText(rs1getString(Cname)trim())
save课号1getText()trim()
}
else{JOptionPaneshowMessageDialog(this没课号课程 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj删){if(savenull)JOptionPaneshowMessageDialog(this没查找需修改课程 )
else{sqldelete from C where Cno'+save+'
sqlSCdelete from SC where Cno'+save+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
stmtexecuteUpdate(sql)
stmtexecuteUpdate(sqlSC)
savenull
JOptionPaneshowMessageDialog(this删完成 )
课号setText()
课名setText()
stmtclose()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
DelSCjava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class DelSC extends JPanel implements ActionListener{
String saveCnull
String saveSnull
JTextField 课号1学号1学号课号成绩
JButton 删查找
public DelSC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
学号1new JTextField(12)
课号1new JTextField(12)
课号new JTextField(12)
学号new JTextField(12)
成绩new JTextField(12)
删new JButton(删)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(学号JLabelCENTER))
box2add(学号)
box3add(new JLabel(成绩JLabelCENTER))
box3add(成绩)
box4add(删)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(查找)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
删addActionListener(this)
查找addActionListener(this)
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1null
String sqlnullsql1null
if(obj查找){if(课号1getText()equals()||学号1getText()equals())JOptionPaneshowMessageDialog(this请填写完成查询信息 )
else{
sql1select * from SC where Cno'+课号1getText()+' and Sno'+学号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){课号setText(rs1getString(Cno)trim())
学号setText(rs1getString(Sno)trim())
成绩setText(rs1getString(C)trim())
saveC课号1getText()trim()
saveS学号1getText()trim()
}
else{JOptionPaneshowMessageDialog(this没课号学生 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj删){if(saveCnull||saveSnull)JOptionPaneshowMessageDialog(this没查找需修改学生课程 )
else{sqldelete from SC where Cno'+saveC+' and Sno'+saveS+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
stmtexecuteUpdate(sql)
saveCnull
saveSnull
JOptionPaneshowMessageDialog(this删完成 )
课号setText()
学号setText()
成绩setText()
stmtclose()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Delstujava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class Delstu extends JPanel implements ActionListener{
String savenull
JTextField 学号1学号姓名系
JButton 删查找
public Delstu(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
学号1new JTextField(12)
学号new JTextField(12)
姓名new JTextField(12)
系new JTextField(12)
删new JButton(删)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(学号JLabelCENTER))
box1add(学号)
box2add(new JLabel(姓名JLabelCENTER))
box2add(姓名)
box3add(new JLabel(系JLabelCENTER))
box3add(系)
box4add(删)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(查找)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
删addActionListener(this)
查找addActionListener(this)
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1null
String sqlnullsql1nullsqlSCnull
if(obj查找){if(学号1getText()equals())JOptionPaneshowMessageDialog(this请填写查询学号 )
else{
sql1select * from S where Sno'+学号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){学号setText(rs1getString(Sno)trim())
姓名setText(rs1getString(Sname)trim())
系setText(rs1getString(Sx)trim())
save学号1getText()trim()
}
else{JOptionPaneshowMessageDialog(this没学号学生 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj删){if(savenull)JOptionPaneshowMessageDialog(this没查找需修改学生 )
else{sqldelete from S where Sno'+save+'
sqlSCdelete from SC where Sno'+save+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
stmtexecuteUpdate(sql)
stmtexecuteUpdate(sqlSC)
savenull
JOptionPaneshowMessageDialog(this删完成 )
学号setText()
姓名setText()
系setText()
stmtclose()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Menujava
import javaawt*
import javaawtevent*
import javaxswing*
public class Menu extends JFrame implements ActionListener{
Addstu 增加学生界面
Updatastu 修改学生界面
Delstu 删学生界面
AddC 增加课程界面
DelC 删课程界面
UpdateC 修改课程界面
AddSC 增加选课界面
DelSC 删选课界面
UpdateSC 修改选课界面
Selstu 学生查询界面
JPanel pCenter
CardLayout cardnull
JLabel labelnull
JMenuBar mbnew JMenuBar()菜单栏
JMenu m1new JMenu(学生理)
JMenuItem add1new JMenuItem(add1 )
JMenuItem updata1new JMenuItem(updata1 )
JMenuItem delete1new JMenuItem(delete1 )
JMenu m2new JMenu(课程理)
JMenuItem add2new JMenuItem(add2 )
JMenuItem updata2new JMenuItem(updata2 )
JMenuItem delete2new JMenuItem(delete2 )
JMenu m3new JMenu(选课理)
JMenuItem add3new JMenuItem(add3 )
JMenuItem updata3new JMenuItem(updata3 )
JMenuItem delete3new JMenuItem(delete3 )
JMenu m4new JMenu(查询理)
JMenuItem 学生查询new JMenuItem(查询信息 )
JMenuItem m5new JMenuItem(系统退出)
Font tnew Font (sanerifFontPLAIN12)
public Menu (){
thissetTitle(学生选课理系统)
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
组合菜单
addMenu1()
addMenu2()
addMenu3()
addMenu4()
addJMenuBar()
setJMenuBar(mb)
labelnew JLabel(选课理系统JLabelCENTER)
labelsetFont(new Font(宋体FontBOLD36))
labelsetHorizontalTextPosition(SwingConstantsCENTER)
labelsetForeground(Colorred)
点击事件
add1addActionListener(this)
updata1addActionListener(this)
delete1addActionListener(this)
m5addActionListener(this)
add2addActionListener(this)
delete2addActionListener(this)
updata2addActionListener(this)
add3addActionListener(this)
delete3addActionListener(this)
updata3addActionListener(this)
学生查询addActionListener(this)
cardnew CardLayout()
pCenternew JPanel()
pCentersetLayout(card)
增加学生界面new Addstu()
修改学生界面new Updatastu()
删学生界面new Delstu()
增加课程界面new AddC()
删课程界面new DelC()
修改课程界面new UpdateC()
增加选课界面new AddSC()
删选课界面new DelSC()
修改选课界面new UpdateSC()
学生查询界面new Selstu()
pCenteradd(欢迎界面label)
pCenteradd(增加学生界面增加学生界面)
pCenteradd(修改学生界面修改学生界面)
pCenteradd(删学生界面删学生界面)
pCenteradd(增加课程界面增加课程界面)
pCenteradd(删课程界面删课程界面)
pCenteradd(修改课程界面修改课程界面)
pCenteradd(增加选课界面增加选课界面)
pCenteradd(删选课界面删选课界面)
pCenteradd(修改选课界面修改选课界面)
pCenteradd(学生查询界面 学生查询界面)
add(pCenterBorderLayoutCENTER)
validate()
setVisible(true)
setBounds(400150600380)
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
addWindowListener(new WindowAdapter(){关闭程序时操作
public void windowClosing(WindowEvent e){Systemexit(0)}
})
validate()
}
private void addJMenuBar() {
mbadd(m1)mbadd(m2)mbadd(m3)mbadd(m4)mbadd(m5)
}
private void addMenu4() {
m4add(学生查询)
m4setFont(t)
}
private void addMenu3() {
m3add(add3)
m3add(updata3)
m3add(delete3)
m3setFont(t)
}
private void addMenu2() {菜单加入菜单栏中
m2add(add2)
m2add(updata2)
m2add(delete2)
m2setFont(t)
}
private void addMenu1() {
m1add(add1)
m1add(updata1)
m1add(delete1)
m1setFont(t)字体
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
if(objm5){Systemexit(0)}
else{if(objadd1){
cardshow(pCenter增加学生界面)
}
else{if(objupdata1){
cardshow(pCenter修改学生界面)
}
else{if(objdelete1){
cardshow(pCenter 删学生界面)
}
else{if(objadd2){
cardshow(pCenter 增加课程界面)
}
else{if(objdelete2){
cardshow(pCenter 删课程界面)
}
else{if(objupdata2){
cardshow(pCenter 修改课程界面)
}
else{if(objadd3){
cardshow(pCenter 增加选课界面)
}
else{if(objdelete3){
cardshow(pCenter 删选课界面)
}
else{if(objupdata3){
cardshow(pCenter 修改选课界面)
}
else{if(obj学生查询){
cardshow(pCenter 学生查询界面)
}}
}}}}}}}}}}
public static void main(String[] args) {
new Menu()
}
SelCjava
import javasql*
import javaawt*
import javaawtevent*
import javaxswing*
import javautil*
public class SelC extends JFrame {
Vector rowData columnNames
Statement stmtnull
String sqlnull
JTable jt null
JScrollPane jsp null
PreparedStatement psnull
ResultSet rsnull
public SelC(String sql1){
columnNames new Vector()
设置列名
columnNamesadd(课号)
columnNamesadd(课名)
rowDatanew Vector()
sqlsql1
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rsstmtexecuteQuery(sql)
while(rsnext()){
Vector hang new Vector()
hangadd(rsgetString(Cno))Systemoutprint(rsgetString(Cno))
hangadd(rsgetString(Cname))Systemoutprint(rsgetString(Cname))
rowDataadd(hang)}
jtnew JTable(rowDatacolumnNames)
jspnew JScrollPane(jt)
thisadd(jsp)
thissetSize(400300)
thissetVisible(true)
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
SelSCjava
import javasql*
import javaxswing*
import javautil*
public class SelSC extends JFrame {
Vector rowData columnNames
Statement stmtnull
String sqlnull
JTable jt null
JScrollPane jsp null
PreparedStatement psnull
ResultSet rsnull
public SelSC(String sql1){
columnNames new Vector()
设置列名
columnNamesadd(课号)
columnNamesadd(课名)
columnNamesadd(学号)
columnNamesadd(姓名)
columnNamesadd(成绩)
rowDatanew Vector()
sqlsql1
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rsstmtexecuteQuery(sql)
while(rsnext()){
Vector hang new Vector()
hangadd(rsgetString(Cno))Systemoutprint(rsgetString(Cno))
hangadd(rsgetString(Cname))Systemoutprint(rsgetString(Cname))
hangadd(rsgetString(Sno))Systemoutprint(rsgetString(Sno))
hangadd(rsgetString(Sname))Systemoutprint(rsgetString(Sname))
hangadd(rsgetString(C))Systemoutprintln(rsgetString(C))
rowDataadd(hang)}
jtnew JTable(rowDatacolumnNames)
jspnew JScrollPane(jt)
thisadd(jsp)
thissetSize(400300)
thissetVisible(true)
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Selstujava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class Selstu extends JPanel implements ActionListener{
JTextField 学号姓名系
JButton 查找
JTextField 课号课名
JButton 查找1
JTextField 课号1学号1成绩
JButton 查找2
public Selstu (){
学号new JTextField(12)
姓名new JTextField(12)
系new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
课号1new JTextField(12)
学号1new JTextField(12)
成绩new JTextField(12)
查找new JButton(查找学生信息)
查找1new JButton(查找课程信息)
查找2new JButton(查找选课信息)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
Box box6BoxcreateHorizontalBox()
Box box7BoxcreateHorizontalBox()
box1add(new JLabel(学号JLabelCENTER))
box1add(学号)
box1add(new JLabel(姓名JLabelCENTER))
box1add(姓名)
box1add(new JLabel(系JLabelCENTER))
box1add(系)
box2add(查找)
box4add(new JLabel(课号JLabelCENTER))
box4add(课号)
box4add(new JLabel(课名JLabelCENTER))
box4add(课名)
box6add(查找1)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(new JLabel(成绩JLabelCENTER))
box5add(成绩)
box7add(查找2)
Box boxH1BoxcreateVerticalBox()竖放box
boxH1add(box1)
boxH1add(box2)
boxH1add(BoxcreateVerticalGlue())
Box boxH2BoxcreateVerticalBox()竖放box
boxH2add(box4)
boxH2add(box6)
boxH2add(BoxcreateVerticalGlue())
Box boxH3BoxcreateVerticalBox()竖放box
boxH3add(box5)
boxH3add(box7)
boxH3add(BoxcreateVerticalGlue())
查找addActionListener(this)
查找1addActionListener(this)
查找2addActionListener(this)
JPanel messPanelnew JPanel()
JPanel picPanelnew JPanel()
JPanel threePanelnew JPanel()
messPaneladd(boxH1)
picPaneladd(boxH2)
threePaneladd(boxH3)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
JSplitPane splitV1new JSplitPane(JSplitPaneVERTICAL_SPLITsplitVthreePanel)分割
add(splitV1BorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent c){
Object objcgetSource()
Statement stmtnull
ResultSet rsnull
int row0
int i0
String sqlnull
Students K
SelC K1
SelSC K2
if(obj查找){
if(学号getText()equals()&&姓名getText()equals()&&系getText()equals()){
sqlselect * from S Systemoutprint(000)000
}
else{
if(学号getText()equals()){
if(姓名getText()equals()){
sqlselect * from S where Sx like'+系getText()+'Systemoutprint(001)}
else{if(系getText()equals()){sqlselect * from S where Sname like'+姓名getText()+'Systemoutprint(010)}
else{sqlselect * from S where Sname like'+姓名getText()+'and Sx like'+系getText()+'Systemoutprint(011)}}}
else{if(姓名getText()equals()){
if(系getText()equals()){sqlselect * from S where Sno like'+学号getText()+'Systemoutprint(100)}
else{sqlselect * from S where Sno like'+学号getText()+' and Sx like'+系getText()+'Systemoutprint(101)}}
else{if(系getText()equals()){sqlselect * from S where Sno like'+学号getText()+' and Sname like'+姓名getText()+'Systemoutprint(110)}
else{sqlselect * from S where Sno like'+学号getText()+' and Sname like'+姓名getText()+' and Sx like'+系getText()+'Systemoutprint(111)}}}
}
Knew Students(sql)
}
else{if(obj查找1){
if(课号getText()equals()&&课名getText()equals()){
sqlselect * from C Systemoutprint(00)00
}
else{
if(课号getText()equals()){sqlselect * from C where Cname like'+课名getText()+'Systemoutprint(01)}
else{if(系getText()equals()){sqlselect * from C where Cno like'+课号getText()+'Systemoutprint(10)}
else{sqlselect * from C where Cno like'+课号getText()+' and Cname like'+姓名getText()+'Systemoutprint(11)}
}
}
K1new SelC(sql)}
else{if(obj查找2){
if(课号1getText()equals()&&学号1getText()equals()&&成绩getText()equals()){
sqlselect SCCnoCnameSCSnoSnameC from SCCS where CCnoSCCno and SSnoSCSnoSystemoutprint(000)000
}
else{
if(课号1getText()equals()){
if(学号1getText()equals()){
sqlselect SCCnoCnameSCSnoSnameC from SCCS where C like'+成绩getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(001)}
else{if(成绩getText()equals()){sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCSno like'+学号1getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(010)}
else{sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCSno like'+学号1getText()+'and C like'+成绩getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(011)}}}
else{if(学号1getText()equals()){
if(成绩getText()equals()){sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCCno like'+课号1getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(100)}
else{sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCCno like'+课号1getText()+' and C like'+成绩getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(101)}}
else{if(成绩getText()equals()){sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCCno like'+课号1getText()+' and SCSno like'+学号1getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(110)}
else{sqlselect SCCnoCnameSCSnoSnameC from SCCS where SCCno like'+课号1getText()+' and SCSno like'+学号1getText()+' and C like'+成绩getText()+' and CCnoSCCno and SSnoSCSnoSystemoutprint(111)}}}
}
K2new SelSC(sql)
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Studentjava
import javasql*
import javaxswing*
import javautil*
public class Students extends JFrame {
Vector rowData columnNames
Statement stmtnull
String sqlnull
JTable jt null
JScrollPane jsp null
PreparedStatement psnull
ResultSet rsnull
public Students(String sql1){
columnNames new Vector()
设置列名
columnNamesadd(学号)
columnNamesadd(姓名)
columnNamesadd(性)
rowDatanew Vector()
sqlsql1
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rsstmtexecuteQuery(sql)
while(rsnext()){
Vector hang new Vector()
hangadd(rsgetString(Sno))Systemoutprint(rsgetString(Sno))
hangadd(rsgetString(Sname))Systemoutprint(rsgetString(Sname))
hangadd(rsgetString(Sx))Systemoutprintln(rsgetString(Sx))
rowDataadd(hang)}
jtnew JTable(rowDatacolumnNames)
jspnew JScrollPane(jt)
thisadd(jsp)
thissetSize(400300)
thissetVisible(true)
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Updatastujava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class Updatastu extends JPanel implements ActionListener{
String savenull
JTextField 学号1学号姓名系
JButton 修改查找
public Updatastu(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
学号1new JTextField(12)
学号new JTextField(12)
姓名new JTextField(12)
系new JTextField(12)
修改new JButton(修改)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(学号JLabelCENTER))
box1add(学号)
box2add(new JLabel(姓名JLabelCENTER))
box2add(姓名)
box3add(new JLabel(系JLabelCENTER))
box3add(系)
box4add(修改)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(查找)
修改addActionListener(this)
查找addActionListener(this)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1null
String sqlnullsql1nullsqlSC
if(obj查找){if(学号1getText()equals())JOptionPaneshowMessageDialog(this请填写查询学号 )
else{
sql1select * from S where Sno'+学号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){学号setText(rs1getString(Sno)trim())
姓名setText(rs1getString(Sname)trim())
系setText(rs1getString(Sx)trim())
save学号1getText()
}
else{JOptionPaneshowMessageDialog(this没学号学生 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj修改){if(savenull){JOptionPaneshowMessageDialog(this没查找需修改学生 )}
else{
if(学号getText()equals()||姓名getText()equals()||系getText()equals()){
JOptionPaneshowMessageDialog(this学生信息填满修改 )
}
else{sqlupdate S set Sno'+学号getText()+'Sname'+姓名getText()+'Sx'+系getText()+'+where Sno'+save+'
if(savetrim()equals(学号getText()trim())){
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
stmtexecuteUpdate(sql)
savenull
JOptionPaneshowMessageDialog(this修改完成 )
学号setText()
姓名setText()
系setText()
stmtclose()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
else{sql1select * from S where Sno'+学号getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){ JOptionPaneshowMessageDialog(this已存学号学生 )
}
else{
sqlSCupdate SC set Sno'+学号getText()+' where Sno'+save+'
stmtexecuteUpdate(sql)
stmtexecuteUpdate(sqlSC)
savenull
JOptionPaneshowMessageDialog(null修改完成 )
学号setText()
姓名setText()
系setText()}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}}}}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
UpdateCjava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class UpdateC extends JPanel implements ActionListener{
String savenull
JTextField 课号1课号课名
JButton 修改查找
public UpdateC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
课号1new JTextField(12)
课号new JTextField(12)
课名new JTextField(12)
修改new JButton(修改)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(课名JLabelCENTER))
box2add(课名)
box3add(修改)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(查找)
修改addActionListener(this)
查找addActionListener(this)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1null
String sqlnullsql1nullsqlSCnull
if(obj查找){if(课号1getText()equals())JOptionPaneshowMessageDialog(this请填写查询课号 )
else{
sql1select * from C where Cno'+课号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){课号setText(rs1getString(Cno)trim())
课名setText(rs1getString(Cname)trim())
save课号1getText()
}
else{JOptionPaneshowMessageDialog(this没课号课程 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj修改){if(savenull){JOptionPaneshowMessageDialog(this没查找需修改课程 )}
else{
if(课号getText()equals()||课名getText()equals()){
JOptionPaneshowMessageDialog(this课程信息填满修改 )
}
else{sqlupdate C set Cno'+课号getText()+'Cname'+课名getText()+' where Cno'+save+'
if(savetrim()equals(课号getText()trim())){
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
stmtexecuteUpdate(sql)
savenull
JOptionPaneshowMessageDialog(this修改完成 )
课号setText()
课名setText()
stmtclose()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
else{sql1select * from C where Cno'+课号getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){ JOptionPaneshowMessageDialog(this已存课号课程 )
}
else{sqlSCupdate SC set Sno'+课号getText()+' where Cno'+save+'
stmtexecuteUpdate(sql)
stmtexecuteUpdate(sqlSC)
savenull
JOptionPaneshowMessageDialog(null修改完成 )
课号setText()
课名setText()}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}}}}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
UpdateSCjava
import javaawt*
import javaawtevent*
import javasql*
import javaxswing*
public class UpdateSC extends JPanel implements ActionListener{
String saveCnull
String saveSnull
JTextField 课号1学号1学号课号成绩
JButton 修改查找
public UpdateSC(){
try {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())}
catch(Exception e){Systemerrprintln(设置外观 +e)}
学号1new JTextField(12)
课号1new JTextField(12)
课号new JTextField(12)
学号new JTextField(12)
成绩new JTextField(12)
修改new JButton(修改)
查找new JButton(查找)
Box box1BoxcreateHorizontalBox()横放box
Box box2BoxcreateHorizontalBox()
Box box3BoxcreateHorizontalBox()
Box box4BoxcreateHorizontalBox()
Box box5BoxcreateHorizontalBox()
box1add(new JLabel(课号JLabelCENTER))
box1add(课号)
box2add(new JLabel(学号JLabelCENTER))
box2add(学号)
box3add(new JLabel(成绩JLabelCENTER))
box3add(成绩)
box4add(修改)
box5add(new JLabel(课号JLabelCENTER))
box5add(课号1)
box5add(new JLabel(学号JLabelCENTER))
box5add(学号1)
box5add(查找)
Box boxHBoxcreateVerticalBox()竖放box
boxHadd(box1)
boxHadd(box2)
boxHadd(box3)
boxHadd(box4)
boxHadd(BoxcreateVerticalGlue())
修改addActionListener(this)
查找addActionListener(this)
JPanel picPanelnew JPanel()
JPanel messPanelnew JPanel()
messPaneladd(box5)
picPaneladd(boxH)
setLayout(new BorderLayout())
JSplitPane splitVnew JSplitPane(JSplitPaneVERTICAL_SPLITmessPanelpicPanel)分割
add(splitVBorderLayoutCENTER)
validate()
}
public void actionPerformed(ActionEvent e){
Object objegetSource()
Statement stmtnull
ResultSet rsnullrs1nullrsCnullrsSnull
String sqlsql1sqlSsqlC
if(obj查找){if(课号1getText()equals()||学号1getText()equals())JOptionPaneshowMessageDialog(this请填写完成查询信息 )
else{
sql1select * from SC where Cno'+课号1getText()+' and Sno'+学号1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rs1stmtexecuteQuery(sql1)
if(rs1next()){课号setText(rs1getString(Cno)trim())
学号setText(rs1getString(Sno)trim())
成绩setText(rs1getString(C)trim())
saveC课号1getText()trim()
saveS学号1getText()trim()
}
else{JOptionPaneshowMessageDialog(this没课号学生 )}
stmtclose()
rs1close()
}catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
else{
if(obj修改){if(saveCnull||saveSnull)JOptionPaneshowMessageDialog(this没查找需修改学生课程 )
else{
if(课号getText()equals()||学号getText()equals()){
JOptionPaneshowMessageDialog(this课程信息填满修改 )
}
else{
sqlCselect * from C where Cno'+课号getText()+'
sqlSselect * from S where Sno'+学号getText()+'
sql1select * from SC where Cno'+课号getText()+' and Sno'+学号getText()+'
sqlupdate SC set Cno'+课号getText()+'Sno'+学号getText()+'C'+成绩getText()+' where Cno'+saveC+' and Sno'+saveS+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rsCstmtexecuteQuery(sqlC)
if(rsCnext()){
rsSstmtexecuteQuery(sqlS)
if(rsSnext()){
if(课号getText()trim()equals(saveC)&& 学号getText()trim()equals(saveS)){
stmtexecuteUpdate(sql)
JOptionPaneshowMessageDialog(this添加成功)
saveCnull
saveSnull
}
else{rs1stmtexecuteQuery(sql1)
if(rs1next()){JOptionPaneshowMessageDialog(this学生课程号存法修改)}
else{
stmtexecuteUpdate(sql)
JOptionPaneshowMessageDialog(this添加成功)
saveCnull
saveSnull
}
rs1close()
}
}
else{JOptionPaneshowMessageDialog(this该学生存法修改)}
rsSclose()
}
else{JOptionPaneshowMessageDialog(this该课程存法修改)}
rsCclose()
stmtclose()
}
catch(SQLException e1){
Systemoutprint(SQL Exception occurMessage is+e1getMessage())
}
}
}
}
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}
Userjava
import javaawt*
import javaawtevent*
import javaxswing*
import javasql*
public class User extends JFrame{
private JLabel usepassword
private JTextField k1户名输入框
private JPasswordField k2密码输入框
private JButton b1b2
登录窗口
public User(JFrame f){
super(系统登录)
Container cgetContentPane()
csetLayout(new FlowLayout())
usenew JLabel(username)
usesetFont(new Font(SerifFontPLAIN20))
passwordnew JLabel(password)
passwordsetFont(new Font(SerifFontPLAIN20))
k1new JTextField(12)
k2new JPasswordField(12)
b1new JButton(登录)
b2new JButton(退出)
设置登录方法
BHandler bnew BHandler()
EXIT dnew EXIT()
b1addActionListener(b)
b2addActionListener(d)
添加控件
cadd(use)
cadd(k1)
cadd(password)
cadd(k2)
cadd(b1)
cadd(b2)
setBounds(600300250150)
setVisible(true)
setResizable(false)
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
}
函数
public static void main(String[] args) {
User f1new User(new JFrame())
}
登录钮方法
private class BHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
if(k1getText()equals()||k2getText()equals()){
JOptionPaneshowMessageDialog(Userthis户名密码空 )
}
else{
Statement stmtnull
ResultSet rsnull
String sql
sqlselect * from admin where username'+k1getText()+'
try{
Connection dbConn1CONN()
stmt(Statement)dbConn1createStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY)
rsstmtexecuteQuery(sql)
if(rsnext()){
String xmrsgetString(password)
if(k2getText()equals(xmtrim())){JOptionPaneshowMessageDialog(Userthis登录成功)
dispose()
new Menu()理窗口
}
else{JOptionPaneshowMessageDialog(Userthis密码错误)}
}
else{JOptionPaneshowMessageDialog(Userthis户名错误)}
rsclose()
stmtclose()
}
catch(SQLException e){
JOptionPaneshowMessageDialog(UserthisSQL Exception occurMessage is+egetMessage())
}
}
}
}
退出方法结束
private class EXIT implements ActionListener{
public void actionPerformed(ActionEvent even){
Systemexit(0)
}
}
连接数库方法
public static Connection CONN(){
String driverName commicrosoftsqlserverjdbcSQLServerDriver 加载JDBC驱动
String dbURL jdbcsqlserverlocalhost1433 DatabaseNamestudent 连接服务器数库test
String userName sa 默认户名
String userPwd huangjiwei 密码
Connection dbConnnull
try {
ClassforName(driverName)
dbConn DriverManagergetConnection(dbURL userName userPwd)
Systemoutprintln(Connection Successful) 果连接成功 控制台输出Connection Successful
} catch (Exception e) {
eprintStackTrace()
}
return dbConn
}
}父类结束
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档