?? indexint.java~145~
字號:
package studentmessage;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class IndexInt
{
JFrame inframe;
JPanel panel, panelup, paneldown, panelcenter,panelcard;
JLabel lblCustID,lblxm,lblNumber,lblUserName,lblTel,lblMail,lblAddr,lblyw,lblsx,lblyy,lblzy;
JLabel lblpic;
GridBagLayout g;
GridBagConstraints c;
JTextField txtCustID,txtxm,txtNumber, txtName,txtTel,txtMail,txtAddr,txtyw,txtsx,txtyy,txtzy;
JButton btnQuery, btnInsert, btnDelete, btnUpdate,btnexit;
BorderLayout bl;
FlowLayout fl;
GridLayout gl;
Icon image;
public IndexInt()
{
inframe=new JFrame("學生信息錄入");
panel = new JPanel();
panelup = new JPanel();
paneldown = new JPanel();
panelcenter = new JPanel();
panelcard=new JPanel();
lblxm=new JLabel("姓名");
txtxm=new JTextField(10);
lblCustID = new JLabel("學生ID");
txtCustID = new JTextField(10);
lblNumber = new JLabel("學號:");
lblUserName = new JLabel("姓名:");
lblTel=new JLabel("電話號碼:");
lblMail=new JLabel("郵箱地址:");
lblAddr=new JLabel("家庭住址:");
lblyw=new JLabel("語文:");
lblsx=new JLabel("數學:");
lblyy=new JLabel("英語:");
lblzy=new JLabel("專業:");
lblpic=new JLabel();
btnQuery = new JButton("查詢");
btnInsert = new JButton("增加");
btnDelete = new JButton("刪除");
btnUpdate = new JButton("修改");
btnexit=new JButton("退出");
txtNumber = new JTextField(10);
txtName = new JTextField(10);
txtTel=new JTextField(10);
txtMail= new JTextField(10);
txtAddr= new JTextField(10);
txtyw=new JTextField(10);
txtsx=new JTextField(10);
txtyy=new JTextField(10);
txtzy=new JTextField(10);
bl = new BorderLayout();
fl = new FlowLayout(FlowLayout.LEFT);
gl = new GridLayout(1, 4);
g = new GridBagLayout();
c = new GridBagConstraints();
image=new ImageIcon("NO.gif");
lblpic=new JLabel(image);
panelcard.add(lblpic);
panelcenter.setLayout(g);
panel.setLayout(bl);
panelup.setLayout(fl);
paneldown.setLayout(gl);
panelup.add(lblCustID);
panelup.add(txtCustID);
panelup.add(lblxm);
panelup.add(txtxm);
panelup.add(btnQuery);
panel.add(panelup, BorderLayout.NORTH);
paneldown.add(btnInsert);
paneldown.add(btnDelete);
paneldown.add(btnUpdate);
paneldown.add(btnexit);
panel.add(paneldown, BorderLayout.SOUTH);
panel.add(panelcard,BorderLayout.EAST);
panel.add(panelcenter, BorderLayout.WEST);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 5;
g.setConstraints(lblNumber, c);
panelcenter.add(lblNumber);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 5;
g.setConstraints(txtNumber, c);
panelcenter.add(txtNumber);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 6;
g.setConstraints(lblUserName, c);
panelcenter.add(lblUserName);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 6;
g.setConstraints(txtName, c);
panelcenter.add(txtName);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 7;
g.setConstraints(lblTel, c);
panelcenter.add(lblTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 7;
g.setConstraints(txtTel, c);
panelcenter.add(txtTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 8;
g.setConstraints(lblMail, c);
panelcenter.add(lblMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 8;
g.setConstraints(txtMail, c);
panelcenter.add(txtMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 9;
g.setConstraints(lblAddr, c);
panelcenter.add(lblAddr);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 9;
g.setConstraints(txtAddr, c);
panelcenter.add(txtAddr);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 10;
g.setConstraints(lblyw, c);
panelcenter.add(lblyw);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 10;
g.setConstraints(txtyw, c);
panelcenter.add(txtyw);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 11;
g.setConstraints(lblsx, c);
panelcenter.add(lblsx);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 11;
g.setConstraints(txtsx, c);
panelcenter.add(txtsx);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 12;
g.setConstraints(lblyy, c);
panelcenter.add(lblyy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 12;
g.setConstraints(txtyy, c);
panelcenter.add(txtyy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 13;
g.setConstraints(lblzy, c);
panelcenter.add(lblzy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 13;
g.setConstraints(txtzy, c);
panelcenter.add(txtzy);
inframe.getContentPane().add(panel);
Query q = new Query();
btnQuery.addActionListener(q);
btnInsert.addActionListener(q);
btnDelete.addActionListener(q);
btnUpdate.addActionListener(q);
btnexit.addActionListener(q);
}
class Query implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource(); //獲取引發事件的事件源
if (obj==btnQuery)
{
if(txtCustID.getText().length()<=0 && txtxm.getText().length()<=0 )
{
JOptionPane.showMessageDialog(inframe,"請輸入要查詢的學號或姓名");
}
else
{
String strCustID = txtCustID.getText().trim();
String strxm = txtxm.getText().trim();
try
{ //加載驅動程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//連接數據庫
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
//執行SQL語句
String sql = "select * from Tabel where 學號=? or 姓名=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, strCustID);
stmt.setString(2, strxm);
ResultSet rs = stmt.executeQuery();
if (rs.next())
{
txtNumber.setText(rs.getString(1));
txtName.setText(rs.getString(2));
txtTel.setText(rs.getString(3));
txtMail.setText(rs.getString(4));
txtAddr.setText(rs.getString(5));
txtyw.setText(rs.getString(6));
txtsx.setText(rs.getString(7));
txtyy.setText(rs.getString(8));
txtzy.setText(rs.getString(9));
txtCustID.setText("");
txtxm.setText("");
con.close();
}
else
{
JOptionPane.showMessageDialog(inframe, "沒有您查詢的學生信息!");
txtCustID.setText("");
}
}
catch (ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(inframe, e1.getMessage());
}
catch (SQLException e2)
{
JOptionPane.showMessageDialog(inframe, e2.getMessage());
}
}
}
if(obj==btnInsert)
{
Add add =new Add();
add.addframe.setSize(200,300);
add.addframe.setVisible(true);
add.addframe.show();
inframe.setVisible(false);
}
if(obj==btnDelete)
{
Delete d=new Delete();
d.delframe.setVisible(true);
d.delframe.setSize(250,150);
d.delframe.show();
inframe.setVisible(false);
}
if(obj==btnUpdate)
{
try
{
String strNumber=txtNumber.getText().trim();
String strName=txtName.getText().trim();
String strTel=txtTel.getText().trim();
String strMail=txtMail.getText().trim();
String strAddr=txtAddr.getText().trim();
String stryw=txtyw.getText().trim();
String strsx = txtsx.getText().trim();
String stryy = txtyy.getText().trim();
String strzy=txtzy.getText().trim();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
String sql = "Update Tabel Set 姓名=?,電話號碼=?,Email=?,家庭住址=?,語文=?,數學=?,英語=?,專業=? where 學號=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,strName);
stmt.setString(2,strTel);
stmt.setString(3,strMail);
stmt.setString(4,strAddr);
stmt.setString(5,stryw);
stmt.setString(6,strsx);
stmt.setString(7,stryy);
stmt.setString(8,strzy);
stmt.setString(9,strNumber);
ResultSet rs;
int i = stmt.executeUpdate();
if(i!=0)
{
JOptionPane.showMessageDialog(inframe, "修改記錄成功!");
con.close();
txtNumber.setText("");
txtName.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
txtyw.setText("");
txtsx.setText("");
txtyy.setText("");
txtzy.setText("");
con.close();
}
else
{
JOptionPane.showMessageDialog(inframe, "修改記錄失敗!");
txtNumber.setText("");
txtName.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
txtyw.setText("");
txtsx.setText("");
txtyy.setText("");
txtzy.setText("");
}
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(inframe,e1.getMessage());
}
catch(SQLException e2)
{
JOptionPane.showMessageDialog(inframe,e2.getMessage());
}
}
if(obj==btnexit)
{
System.exit(0);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -