📄 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 + -