?? jpanel3.java
字號:
package guanlixitong;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.*;
import java.sql.*;
public class JPanel3
extends JPanel implements ActionListener {
public JPanel3() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Student st = new Student();
private void jbInit() throws Exception {
this.setLayout(null);
jTextField1.setBounds(new Rectangle(237, 81, 166, 28));
jLabel4.setText("班 級 ");
jLabel4.setBounds(new Rectangle(133, 306, 64, 29));
jLabel4.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel4.setForeground(Color.magenta);
jLabel5.setText("電 話");
jLabel5.setBounds(new Rectangle(133, 256, 61, 29));
jLabel5.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel5.setForeground(Color.magenta);
jLabel6.setBounds(new Rectangle(133, 209, 61, 29));
jLabel6.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel6.setForeground(Color.magenta);
jLabel6.setText("地 址");
jLabel7.setText("姓 名");
jLabel7.setBounds(new Rectangle(133, 165, 61, 29));
jLabel7.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel7.setForeground(Color.magenta);
jLabel8.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel8.setForeground(Color.magenta);
jLabel8.setText("性 別");
jLabel8.setBounds(new Rectangle(133, 122, 61, 29));
jTextField2.setBounds(new Rectangle(237, 165, 166, 28));
jTextField3.setText(" ");
jTextField3.setBounds(new Rectangle(237, 209, 253, 28));
jTextField4.setBounds(new Rectangle(237, 256, 253, 28));
jTextField5.setText(" ");
jTextField5.setBounds(new Rectangle(237, 306, 253, 28));
jButton1.setBounds(new Rectangle(133, 369, 144, 32));
jButton1.setFont(new java.awt.Font("宋體", Font.PLAIN, 16));
jButton1.setForeground(Color.magenta);
jButton1.setText("錄 入");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton2.setText("查 詢");
jButton2.setForeground(Color.magenta);
jButton2.setFont(new java.awt.Font("宋體", Font.PLAIN, 16));
jButton2.setBounds(new Rectangle(346, 369, 144, 32));
jLabel1.setFont(new java.awt.Font("宋體", Font.PLAIN, 15));
jLabel1.setForeground(Color.magenta);
jLabel1.setText("學 號");
jLabel1.setBounds(new Rectangle(133, 81, 61, 29));
jTextField6.setBounds(new Rectangle(237, 122, 166, 28));
jLabel2.setFont(new java.awt.Font("宋體", Font.PLAIN, 16));
jLabel2.setForeground(Color.red);
jTextField1.addActionListener(this);
jLabel2.setBounds(new Rectangle(133, 30, 331, 29));
this.add(jButton2, null);
this.add(jButton1);
this.add(jLabel4);
this.add(jLabel5);
this.add(jLabel6);
this.add(jLabel7);
this.add(jTextField5);
this.add(jTextField4);
this.add(jTextField3);
this.add(jTextField2);
this.add(jTextField1);
this.add(jLabel1);
this.add(jLabel8);
this.add(jTextField6);
this.add(jLabel2);
}
JTextField1 jTextField1 = new JTextField1();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextField6 = new JTextField();
JLabel jLabel2 = new JLabel();
public void actionPerformed(ActionEvent e) {
jLabel2.setText("");
if (e.getSource() == jButton1||e.getSource()==jTextField1) {
int num=0;
if(!jTextField1.getText().equals(""))
num = Integer.parseInt(jTextField1.getText().trim());
String url = "jdbc:odbc:yangAcess";
Connection con;
Statement stmt;
String str = "select 學號,姓名,地址,電話,班級,性別 from yang1 where 學號=" + num;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement();
rs = stmt.executeQuery(str);
while (rs.next()) {
st.學號 = rs.getInt(1);
st.姓名 = rs.getString(2);
st.地址 = rs.getString(3);
st.電話 = rs.getString(4);
st.班級 = rs.getString(5);
st.性別 = rs.getString(6);
}
}
catch (Exception ae) {System.out.print("you");}
if (num == st.學號&&!st.性別.equals("")) {
ShowFrame show=new ShowFrame();
show.setVisible(true);
}
else {
getDate();
insert();
}
}
else if (e.getSource() == jButton2) {
int num=0;
if(!jTextField1.getText().equals(""))
num= Integer.parseInt(jTextField1.getText());
String url = "jdbc:odbc:yangAcess";
Connection con;
Statement stmt;
String str = "select 學號,姓名,地址,電話,班級,性別 from yang1 where 學號=" + num;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement();
rs = stmt.executeQuery(str);
while (rs.next()) {
st.學號 = rs.getInt(1);
st.姓名 = rs.getString(2);
st.地址 = rs.getString(3);
st.電話 = rs.getString(4);
st.班級 = rs.getString(5);
st.性別 = rs.getString(6);
}
}
catch (Exception ae) {}
if (st.學號 == num) {
jTextField1.setText(Integer.toString(st.學號));
jTextField2.setText(st.姓名);
jTextField3.setText(st.地址);
jTextField4.setText(st.電話);
jTextField5.setText(st.班級);
jTextField6.setText(st.性別);
}
else {
jLabel2.setText("查 不 到 你 所 輸 學 號 信 息");
}
}
}
private class ShowFrame
extends JFrame implements ActionListener {
JButton btn1, btn2;
public ShowFrame() {
this.setSize(400, 200);
this.setLocation(200, 200);
Container con = this.getContentPane();
con.setLayout(new FlowLayout());
JLabel jb = new JLabel("此學號已存在,你想重置嗎?");
jb.setForeground(Color.magenta);
jb.setFont(new java.awt.Font("Dialog", 0, 25));
btn1 = new JButton("確 定");
btn1.setForeground(Color.magenta);
btn1.setFont(new java.awt.Font("Dialog", 0, 15));
btn2 = new JButton("取 消");
btn2.setForeground(Color.magenta);
btn2.setFont(new java.awt.Font("Dialog", 0, 15));
con.add(jb);
con.add(btn1);
con.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
this.setVisible(false);
getDate();
updata();
}
else if (e.getSource() == btn2) {
this.setVisible(false);
}
}
}
public void updata() {
if (st.學號 != 0) {
String str = "jdbc:odbc:yangAcess";
Connection con;
// String str1 =
//"update yang1 set 姓名='"+std.姓名+"',成績1="+std.成績1+",成績2="+std.成績2+",成績3="+std.成績3+
//",成績4="+std.成績4+",成績5="+std.成績5+ "where 學號="+std.學號;
String str1 =
"update yang1 set 姓名='" + st.姓名 + "',性別='" + st.性別 + "',地址 ='"+
st.地址 + "',班級='" + st.班級 + "',電話='" + st.電話 +
"' where 學號=" + st.學號;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(str);
Statement update = con.createStatement();
/**********************************************
update.setString(1, std.姓名);
update.setInt(2, std.成績1);
update.setInt(3, std.成績2);
update.setInt(4, std.成績3);
update.setInt(5, std.成績4);
update.setInt(6, std.成績5);
update.setInt(7, std.學號);
******************************************/
update.executeUpdate(str1);
con.close();
update.close();
}
catch (Exception ae) {
System.out.print("lk;ag;oiuje");
}
}
}
public void insert() {
if (st.學號 != 0) {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ur1 = "jdbc:odbc:yangAcess";
Connection conn;
int age = 0;
Statement statement;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(ur1);
PreparedStatement pstmt = conn.prepareStatement(
"insert into yang1 values(?,?,?,0,0,0,0,0,?,?,?,0,0,0,0,0)");
{
pstmt.setInt(1, st.學號);
pstmt.setString(2, st.姓名);
pstmt.setString(3, st.性別);
pstmt.setString(4, st.地址);
pstmt.setString(5, st.電話);
pstmt.setString(6, st.班級);
}
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
catch (Exception e) {
System.out.print("I");
}
}
}
public void getDate() {
if (!jTextField1.getText().equals("")) {
st.學號=Integer.parseInt(jTextField1.getText());
}
if (!jTextField2.getText().equals("")) {
st.姓名=jTextField2.getText();
}
if (!jTextField3.getText().equals("")) {
st.地址=jTextField3.getText();
}
if (!jTextField4.getText().equals("")) {
st.電話=jTextField4.getText();
}
if (!jTextField5.getText().equals("")) {
st.班級=jTextField5.getText();
}
if (!jTextField6.getText().equals("")) {
st.性別=jTextField6.getText();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -