?? studentdb.java.bak
字號:
{
e.printStackTrace();
}
execuiteQuery();
}
public void actionPerformed(ActionEvent e) //監聽
{
String strMenuItem =new String(e.getActionCommand());
if(strMenuItem.equals("退出(X)")||e.getSource()==btnexit||strMenuItem.equals("關閉數據庫")) //退出系統或關閉數據庫
{ //System.exit(0);
try
{ if(conn !=null)
{
stmt.close();
conn.close();
}
}
catch(SQLException ev)
{ System.err.println("Unable to disconnect");
}
btnexit.setLabel("連接數據庫");
if(strMenuItem.equals("退出(X)")) System.exit(0);
}
if(strMenuItem.equals("連接數據庫")) //連接數據庫
{ //System.out.println("hello");
btnexit.setLabel("關閉數據庫");
try
{
conn = getConnection();
stmt = conn.createStatement();
}
catch(SQLException eve)
{
while(e != null)
{
eve.printStackTrace();
eve=eve.getNextException();
}
}
catch(IOException eve)
{
//System.err.println("erro");
eve.printStackTrace();
}
execuiteQuery();
//System.out.println("goodbye");
}
if(strMenuItem.equals("版本信息")) //幫助信息
{
//txaResult.setText("版本:1.10 作者:宋小林 指導教師:叉叉");
try {
SwingUtilities.invokeLater(new Runnable(){ //調用的change界面上面的代碼的時候
// event派發線程已經中止代來的問題
public void run() {
txaResult.setText("版本:1.10 作者:宋小林 指導教師:叉叉");
//txaResult.setText("查詢時請輸入學號");
}
});
} catch(Exception ew) {
ew.printStackTrace(System.out);
}
return;
}
if(e.getSource()==btnreset) //重置
{
txtStuno.setText("");
txtStuname.setText("");
txtStudept.setText("");
txtStuage.setText("");
txtStuhometown.setText("");
cbxStusex.setSelectedIndex(0);
cbxStupolitical.setSelectedIndex(0);
}
if(e.getSource()==btnquery||strMenuItem.equalsIgnoreCase("查詢(Q)")) //查詢
{
// StuQuery();
String sno=txtStuno.getText().trim();
if(sno.equals(""))
{
txaResult.setText("查詢時請輸入學號");
return;
}
try
{
ResultSet rs =
stmt.executeQuery("SELECT * FROM Student where 學號="+sno);
if(rs.next())
{
txtStuname.setText(rs.getString("姓名"));
txtStuage.setText(rs.getString("年齡"));
cbxStusex.setSelectedItem(rs.getString("性別"));
cbxStupolitical.setSelectedItem(rs.getString("政治面貌"));
txtStuhometown.setText(rs.getString("籍貫"));
txtStudept.setText(rs.getString("所屬系"));
rs.close();
}
else
{
txaResult.setText("未找到該生信息!");
txtStuname.setText("");
txtStuage.setText("");
cbxStusex.setSelectedItem("");
cbxStupolitical.setSelectedItem("無");
txtStuhometown.setText("");
txtStudept.setText("");
return;
}
}
catch(SQLException ex)
{
txaResult.setText("查詢出錯");
}
}
if(e.getSource()==btnInsert||strMenuItem.equalsIgnoreCase("插入(I)")) //插入
{
//StuInsert();
String sno = txtStuno.getText().trim();
String name = txtStuname.getText().trim();
Integer age =
txtStuage.getText().trim().equals("")?
null:new Integer(txtStuage.getText().trim());
if(sno.equals("")||name.equals(""))
{
txaResult.setText("學號和姓名不能為空");
return;
}
try
{
ResultSet rs =
stmt.executeQuery("SELECT * FROM Student where 學號="+sno);
if(rs.next())
{
JOptionPane.showMessageDialog(null, "此學號已經被注冊.", "警告",
JOptionPane.WARNING_MESSAGE);
return;
}
stmt.execute(
"insert into student(學號,姓名,性別,年齡,政治面貌,籍貫,所屬系) values('"
+sno+"','"+name+"','"+cbxStusex.getSelectedItem()+"',"+
age+",'"+
cbxStupolitical.getSelectedItem()+"','"+
txtStuhometown.getText()+"','"+txtStudept.getText()+"')");
JOptionPane.showMessageDialog(null, "增加信息成功!");
}
catch(SQLException ex)
{
txaResult.setText("插入記錄出錯");
}
}
if(e.getSource()==btndelete||strMenuItem.equalsIgnoreCase("刪除(D)")) //刪除
{
//StuDelete();
String sno = txtStuno.getText().trim();
if(sno.equals(""))
{
txaResult.setText("請輸入要刪除學生記錄的學號");
return;
}
try
{
ResultSet rs =
stmt.executeQuery("SELECT * FROM Student where 學號= "+sno );
if(!rs.next())
{
JOptionPane.showMessageDialog(null, "此學號未注冊!");
return;
}
if (JOptionPane.showConfirmDialog(this,
"確實要刪除該生信息嗎?\n刪除的信息將不能恢復,繼續?",
"刪除確定", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE) == 0) {
stmt.execute("DELETE FROM Student where 學號="+sno);
JOptionPane.showMessageDialog(null, "刪除成功!");
}
}
catch(SQLException ex)
{
while(ex != null)
{
ex.printStackTrace();
ex=ex.getNextException();
}
txaResult.setText("刪除記錄出錯");
}
}
if(e.getSource()==btnmodify||strMenuItem.equalsIgnoreCase("修改(M)")) //修改
{
//StuModify();
String sno = txtStuno.getText().trim();
String name = txtStuname.getText().trim();
Integer age =
txtStuage.getText().trim().equals("")?
null:new Integer(txtStuage.getText().trim());
if(sno.equals("")||name.equals(""))
{
txaResult.setText("學號和姓名不能為空!");
//JOptionPane.showMessageDialog(null, "學號和姓名不能為空!");
return;
}
try
{
ResultSet rs =
stmt.executeQuery("SELECT * FROM Student where 學號="+sno);
if(!rs.next())
{
txaResult.setText("沒有該生信息,無法更新");
return;
}
String sql=new String(
"update student set 姓名='"+name+"',性別='"+cbxStusex.getSelectedItem()
+"',年齡="+age+",政治面貌='"+cbxStupolitical.getSelectedItem()
+"',籍貫='"+txtStuhometown.getText()+"',所屬系='"+txtStudept.getText()+"'"
+" where 學號="+sno
);
stmt.execute(sql);
JOptionPane.showMessageDialog(null, "修改信息成功!");
}
catch(SQLException ex)
{
while(ex != null)
{
ex.printStackTrace();
ex=ex.getNextException();
}
txaResult.setText("修改記錄出錯");
}
}
execuiteQuery();
}
/* 擴展用
public void StuQuery()
{
String sno=txtStuno.getText();//.trim();
if(sno.equals(""))
{
try {
SwingUtilities.invokeLater(new Runnable(){ //調用的change界面上面的代碼的時候
// event派發線程已經中止代來的問題
public void run() {
txaResult.setText("查詢時請輸入學號");
}
});
} catch(Exception ew) {
ew.printStackTrace(System.out);
}
return;
}
/*if(sno.equals(""))
{
txaResult.setText("查詢時請輸入學號");
// System.out.println("you win");
return;
}
try
{
ResultSet rs =
stmt.executeQuery("SELECT * FROM Student where 學號="+sno);
if(rs.next())
{
txtStuname.setText(rs.getString("姓名"));
txtStuage.setText(rs.getString("年齡"));
cbxStusex.setSelectedItem(rs.getString("性別"));
cbxStupolitical.setSelectedItem(rs.getString("政治面貌"));
txtStuhometown.setText(rs.getString("籍貫"));
txtStudept.setText(rs.getString("所屬系"));
rs.close();
}
else
{
txaResult.setText("未找到該生信息!");
txtStuname.setText("");
txtStuage.setText("");
cbxStusex.setSelectedItem("");
cbxStupolitical.setSelectedItem("無");
txtStuhometown.setText("");
txtStudept.setText("");
//return;
}
}
catch(SQLException ex)
{
txaResult.setText("查詢出錯");
}
}
public void StuDelete()
{
}
public void StuInsert()
{
}
public void StuModify()
{
}
*/
public static Connection getConnection() //裝載數據庫驅動
throws SQLException,IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if(drivers != null)
{
/*通過系統調用設置jdbc 驅動*/
/*也可通過Class.forName的方式注冊驅動*/
System.setProperty("jdbc.drivers",drivers);
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url,username,password);
}
private void execuiteQuery()
{
ResultSet rs = null;
txaResult.setText("");
txaResult.append("學號 姓名 性別 年齡 政治面貌 籍貫 所屬系\n");
try
{
rs = stmt.executeQuery("SELECT * FROM student");
while(rs.next())
{
txaResult.append(rs.getString("學號")+"\t");
txaResult.append(rs.getString("姓名")+"\t");
txaResult.append(rs.getString("性別")+"\t");
txaResult.append(rs.getString("年齡")+"\t");
txaResult.append(rs.getString("政治面貌")+"\t");
txaResult.append(rs.getString("籍貫")+"\t");
txaResult.append(rs.getString("所屬系")+"\t");
txaResult.append("\n");
}
rs.close();
}
catch(SQLException e)
{
while(e != null)
{
e.printStackTrace();
e=e.getNextException();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -