学生管理系统java实现.docx
《学生管理系统java实现.docx》由会员分享,可在线阅读,更多相关《学生管理系统java实现.docx(28页珍藏版)》请在冰豆网上搜索。
学生管理系统java实现
学生管理系统java实现
准备的软件,我用是eclipse.exe,MySql,还有mysqljdbc5.0数据库。
网上很容易找到华军上就有的。
第一步,安装eclipse.exe,MySql。
MySql的密码请设置为123,因为我的程序上的密码就
是123。
以后你看懂了可以修改的!
把下载的mysqljdbc5.0数据库.zip通常是个压缩包解压到一个地方。
然后运行eclipse.exe。
建立个项目,名字自己随便叫即可。
然后在菜单栏上Project就是Run旁边的那个菜单,按下左键选择Properties就是最下面的一项。
选择JavaBuildPath。
在对话框的右面出现个小窗体,选Libraries。
选择AddExternalJARs...。
选择你mysqljdbc5.0数据库,解压出来的mysql-connector-java-5.0.0-beta-bin.jar也许名字有出入。
如下面的图
添加完成后就是这个样子了
第二步安装Mysql数据库请把密码设置为123就是带password的地方。
我的程序上密码设置就是123为了运行方便。
然后运行Mysql执行下面四个步骤
贏MySQLCommandLineClient
EnteP*****
7eleonetotheJI^SQLnanitor,GomnAnd^endwit^our*MySQLconnectionidis15
Seinerversion:
S_0_6?
-coinniuriity-nttlySQIrGommur
rypeJhelp;*or'\h‘forhelp,Type'y'toelec
createdatscxdb;
n^sqi/rnieql>ruitsql\nysql>rn^sql>mysql>mysql>usescxdb;
nysqT?
Lyeql>rnysijLycreatetablestudentK
>nvmbepirit,->n^nechar<8>f—>LAnguasfeint,—>rtathlnt»->en^lishint,->birthdavdat&>;.
[ijit«ih4・♦elwirig^il
Uiert坯切ttvdentTiWe■「许LWJ■fft・”・初‘」‘9出J即‘J沾阳刖«lJ>t.
四部的命令分别是
1。
createdatabasescxdb;
2。
usescxdb;
3。
createtablestudent(numberint,
namechar(8),
Ianguageint,
mathint,englishint,birthdaydate);
4。
insertintostudentvalues('1717','曹操','80','90','20','1985-01-01');下面是程序清单!
//=====================================================================
第一部分
packageDBTest;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JFrame;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;
publicclassDBFrame{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Stringsql="select*fromstudent;";
//sql="insertintostudentvalues
//('1717','张云风','56','80','65','1979-02-16');";
DBFrameWindowdb=newDBFrameWindow(sql);db.setDefaultCloseOperation(db.EXIT_ON_CLOSE);db.setVisible(true);
db.setResizable(false);
}
}
classDBFrameWindowextendsJFrame{
publicDBFrameWindow(Stringsql){super.setTitle("DBFrameWindow");this.sql=sql;
this.setSize(500,380);
JTabletable=DBHander.getTable(this.sql);
scroolPane=newJScrollPane(table);this.add(scroolPane);
//============================================
JMenurunMenu=newJMenu("Run");
JMenuBarmb=newJMenuBar();
mb.add(runMenu);
this.setJMenuBar(mb);//=============Append===============Append===============JMenuItemappendItem=newJMenuItem("Append");appendItem.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstubappendFrameaf=newappendFrame();af.setDefaultCloseOperation(af.EXIT_ON_CLOSE);af.setVisible(true);
af.setResizable(false);DBHander.getUpdataTest("");
}
});
//=============Append===============Append===============
//=============Remove===============Remove===============JMenuItemremoveItem=newJMenuItem("Remove");
removeItem.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstubremoveFramerf=newremoveFrame();rf.setDefaultCloseOperation(rf.EXIT_ON_CLOSE);rf.setResizable(false);
rf.setVisible(true);
}
});
//=============Remove===============Remove===============
//=============Update===============UpdateJMenuItemupdateItem=newJMenuItem("Update");updateItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstubupdateFrameuf=newupdateFrame();uf.setDefaultCloseOperation(uf.EXIT_ON_CLOSE);uf.setResizable(false);
uf.setVisible(true);
});
//=============Update===============Update===========
//=============Find===============Find===============JMenuItemfindItem=newJMenuItem("Find");
findItem.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstubselectFramesf=newselectFrame();sf.setDefaultCloseOperation(sf.EXIT_ON_CLOSE);sf.setResizable(false);
sf.setVisible(true);
}});
//=============Find===============Find===============JMenuItemrefreshItem=newJMenuItem("Refresh");refreshItem.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
JTableta=DBHander.getTable("select*fromstudent;");JScrollPanescroolPane_1=newJScrollPane(ta);
add(scroolPane_1);
System.out.println("refreshItem");
}
});
runMenu.add(appendItem);runMenu.add(removeItem);
runMenu.add(updateItem);runMenu.add(findItem);
runMenu.add(refreshItem);
//============================================
}privateJScrollPanescroolPane=null;privateStringsql="";
}
//=====================================================================
//=====================================================================
第二部分
packageDBTest;
importjava.io.UnsupportedEncodingException;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.ResultSetMetaData;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.swing.JTable;
importcom.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
publicclassDBHander{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Stringsql="insertintostudentvalues('1717','张云风','56','80','65','1979-02-16');";Connectionconn=getConnection();
if(conn==null){
System.out.println("conn==null");
}else{
System.out.println("conn!
=null");
}
Statementstat=getStatement();
if(stat==null){
System.out.println("stat==null");
}else{
System.out.println("stat!
=null");
}
ResultSetrs=getResult("select*fromstudent;");
if(rs==null){
System.out.println("rs==null");
}else{
System.out.println("rs!
=null");
JTabletable=getTable("select*fromstudent;");if(rs==null){
System.out.println("table==null");
}else{
System.out.println("table!
=null");
}
}
privatestaticString[][]select=null;
//===================Connection===================================publicstaticConnectiongetConnection(){
MysqlConnectionPoolDataSourceds=newMysqlConnectionPoolDataSource();ds.setUrl("jdbc:
MySQL:
//localhost/scxdb");
ds.setUser("root");ds.setPassword("123");
try{
returnds.getConnection();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}System.out.println("conn==null");
returnnull;
}
//===================Connection
//===================StatementpublicstaticStatementgetStatement(){
Connectionconn=DBHander.getConnection();
try{
returnconn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println("stat==null");
returnnull;
}
//===================Statement
//===================ResultSet
publicstaticResultSetgetResult(Stringsql){try{
returngetStatement().executeQuery(sql);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}
System.out.println("table==null");
returnnull;
}
//===================ResultSet
//===================getTablepublicstaticJTablegetTable(Stringsql){
ResultSetrs=null;
String[][]str=null;
intcols=0;
try{
rs=getResult(sql);
ResultSetMetaDatamd=rs.getMetaData();
cols=md.getColumnCount();
intn=0;
while(rs.next()){
n=n+1;
}
str=newString[n][cols];
}catch(SQLExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}
rs=getResult(sql);
intn=0;
try{
while(rs.next()){
for(inti=0;i//str[n][i]=rs.getString(i+1);
str[n][i]=newString(rs.getString(i+1).getBytes("iso-8859-1"),"GBK");
}
n=n+1;
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}
String[]s=newString[]{"Number","Name","Language","Math","English","Birthday"};JTabletable=newJTable(str,s);
for(inti=0;ifor(intj=0;jSystem.out.print(str[i][j]+"");
System.out.println();
}
select=str;
returntable;
}
//===================getTable===================================
publicstaticvoidgetUpdataTest(Stringsql){
try{
Connectionconn=getConnection();
if(conn==null)System.out.println("null");
?
WHERE
PreparedStatementps=conn.prepareStatement("UPDATEstudentSETnumber=number=?
;");
ps.setString(1,"2088");
ps.setString(2,"1717");
System.out.println("#################");
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
//===================getUpdata===================================publicstaticvoidgetUpdate(Stringsql){
Statementstat;
try{
stat=getConnection().createStatement();
intn=stat.executeUpdate(sql);
System.out.println("*********"+n+"***********");
}catch(SQLExceptione){
//TODOAuto-generatedcatchblocke.printStackTrace();
}
}
//===================getUpdata
//===================getSelect
publicstaticString[][]getSelect(Stringsql){getTable(sql);
if(select==null)System.out.println("select=null");returnselect;
}
//===================getSelect===================================
}
//=====================================================================
//=====================================================================
第3部分
packageDBTest;
importjava.awt.BorderLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.UnsupportedEncodingException;
im