需要的工具
1.SQL Server
2.Eclipse
3.JDBC連線資料庫驅動
功能實現
分為三大類
1.land.java (登陸介面)
2.system.java (系統管理介面)
3.sql.java (資料庫操作,包括連線、斷開、增刪改查等操作)
一、登陸介面及程式碼
部分程式碼:
登陸按鈕事件
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if((JButton)e.getSource()== buttonLand)
{
String name = textName.getText().trim();
String password = String.valueOf(textPassword.getPassword()).trim();
int num = sq.landing(name, password);
if(num==1)
{
JOptionPane.showMessageDialog(frame, "歡迎進入學生宿舍管理系統!","提示:",JOptionPane.PLAIN_MESSAGE);
system system = new system();
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(frame, "賬號或者密碼錯誤!","提示:",JOptionPane.ERROR_MESSAGE);
}
}
}1234567891011121314151617181920
二、系統管理介面及程式碼**(這裡只新增了倆個表,多少表都是一樣的道理**)
增加資料:點選增加按鈕會出現一空白行,填寫完點選儲存即可
儲存(更新)資料:直接在表中進行相應的操作,再點選儲存即可
刪除資料:選擇某行,點選刪除按鈕即可
部分程式碼:
//--------------------------------------按鈕事件--------------------------------
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//------------------------------- 刪除-----------------------------
String tableName = null;
String key1 = null;
if((JButton)e.getSource()==buttonDelete)
{ if(paneParent.getSelectedIndex()==1)
{
tableName = "學生資訊表";
key1 = "sno";
}
if(paneParent.getSelectedIndex()==2)
{
tableName = "宿舍資訊表";
key1 = "dno";
}
int row = table.getSelectedRow();
if(row!=-1)
{
String key2 = (String) tableModel.getValueAt(row, 0);
int result = JOptionPane.showConfirmDialog(null, "確定要刪除嗎?","請確認",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION)
{
String sql = "delete from "+tableName+" where "+key1+"="+key2;
int num = sq.delete(sql);
if(num>0)
{
tableModel.removeRow(row);
}
}
}
else
{
JOptionPane.showMessageDialog(null, "請選擇要刪除的行!","提示:",JOptionPane.ERROR_MESSAGE);
}
}
//------------------------------儲存------------------------
if((JButton)e.getSource()==buttonSave)
{
int result = JOptionPane.showConfirmDialog(null, "請確認數值已經更改,否則儲存無效","請確認",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION)
{
int row = table.getRowCount();
int column = table.getColumnCount();
String[][]valueRow= new String[row][column];
String[] sqlvalue = new String[row];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
valueRow[i][j] = table.getValueAt(i, j).toString();
}
}
if(paneParent.getSelectedIndex()==1)
{
for(int i =0;i<row;i++)
{
String sql = "insert into 學生資訊表"+" values ("+valueRow[i][0].toString()+","+valueRow[i][1].toString()+","+valueRow[i][2].toString()+","+valueRow[i][3].toString()+","+valueRow[i][4].toString()+","+valueRow[i][5].toString()+")";
sqlvalue[i]=sql.toString();
}
data = sq.Save(sqlvalue,"學生資訊表",row, column);
tableModel.setDataVector(data,dataTitle);
}
if(paneParent.getSelectedIndex()==2)
{
for(int i =0;i<row;i++)
{
String sql = "insert into 宿舍資訊表"+" values ("+valueRow[i][0].toString()+","+valueRow[i][1].toString()+")";
sqlvalue[i]=sql.toString();
}
data = sq.Save(sqlvalue,"宿舍資訊表",row, column);
tableModel.setDataVector(data,TitleDormitor);
}
}
}
//------------------------------增加---------------------------
if((JButton)e.getSource()==buttonIncrease)
{
tableModel.addRow(new Vector<>());
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
三、sql.java
(重要)
public class SQL {
public Connection conn;
public Statement st;
public ResultSet rs;
//----------------------------獲取連結--------------------------------
public Connection getConn() throws SQLException, ClassNotFoundException
{
String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databaseName=學習宿舍";
//:1433為資料庫預設埠號,學習宿舍為資料庫名字
String user = "sa"; //登入使用者名稱
String password = "123456"; //登入密碼
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, user, password);
System.out.println("資料庫連線成功");
} catch (SQLException ex1)
{
System.out.println("資料庫連線失敗");
}
return conn;
}
//-----------------------------------關閉連結----------------------------------
public void Close()
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//----------------------------------------登陸---------------------------------
public int landing(String name1,String password1)
{
int num = 0;
String sql = "select *from 使用者表";
try{
getConn();
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next())
{
String name = rs.getString(1).trim();
String password = rs.getString(2).trim();
if(name.equals(name1)&&password.equals(password1))
{
num = 1;
}
}
}catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return num;
}
//--------------------------------查詢-------------------------------
public Vector<Vector<Object>> query(String tableName,int column)
{
int num = 0;
String sql = "select *from "+tableName;
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try{
getConn();
st = conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
Vector<Object> rowdata = new Vector<Object>();
for(num=1;num<=column;num++)
{
rowdata.add(rs.getString(num));
}
data.add(rowdata);
}
}catch(SQLException ex1)
{
System.out.println("失敗"+ex1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return data;
}
//---------------------------------------刪除----------------------------------
public int delete(String sql)
{
int num = 0;
try{
getConn();
st = conn.createStatement();
num = st.executeUpdate(sql);
}catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return num;
}
//-------------------------------------儲存-------------------------------------
public Vector<Vector<Object>> Save(String[] sqlvalue,String tableName,int row,int column)
{
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try{
getConn();
st = conn.createStatement();
st.executeUpdate("delete from "+tableName);
for(int i =0;i<row;i++)
{
st.executeUpdate(sqlvalue[i].toString());
}
data = query(tableName, column);
}catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
資料庫連線賬號密碼及表名
**
這個是資料庫的連線賬號和密碼123456
/anhui/
這個是使用者表結構和登陸賬號和密碼(可自行修改)
**
宿舍資訊表結構**
/hebei/
**
學生資訊表結構**
/ningxia/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30239065/viewspace-2741469/,如需轉載,請註明出處,否則將追究法律責任。