


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、/* e y?Y?a d ? o 10 ? dd */import java.sql.Connection。import java.sql.DriverManager。import java.sql.ResultSet。import java.sql.SQLException。import java.sql.Statement。public class DB / ?卩?e y?Y?ad ? opublic static Connection getConn(> / x0 2d mysql 卩?jdbc?y?-try Class.forName("com.mysql.jdbc.D
2、river"> catch (ClassNotFoundException e> e.printStackTrace(>。/'' ?"e y?Y?ad ? o ? oString ip = "localhost"。+ dbName 。 /"root",String dbName = "jdbc"。String url = "jdbc:mysql:/"+ ip + "/"'?' | e ?mysql jdbc d ?D- oeC
3、onnection conn = null。try conn = DriverManager.getConnection(url, "1234"> 。 catch (SQLException e> e.printStackTrace(>。return conn 。public static void close(ResultSet rs> if (rs != null> try rs.close(> 。 catch (SQLException e> e.printStackTrace(>。public static void
4、close(Statement pstmt> if (pstmt != null> try pstmt.close(> 。 catch (SQLException e> e.printStackTrace(>。public static void close(Connection conn> if (conn != null> try conn.close(> 。 catch (SQLException e> e.printStackTrace(> public static void rollback(Connection conn
5、> if (conn != null> try conn.rollback(> catch (SQLException e> e.printStackTrace(> /* 刪除類 */ import java.sql.Connection 。import java.sql.SQLException 。import java.sql.Statement 。public class Drop public void drop(MainFrame frame, String number, String choice> Connection con = null
6、。 Statement s = null 。con = try DB.getConn(> 。if(number.isEmpty(>>frame.text.append(" 關 鍵 字 不 能 為 空 ! 刪 除 失 敗 !nn"> 。return 。s = con.createStatement(> 。String str = "delete from student where " + choice + "='"+ numberI Hill+ "'" 。try s.e
7、xecuteUpdate(str> 。frame.text.append(" 刪除完成! nn"> 。 catch (Exception e> / TODO Auto-generated catch block frame.text.append(" 刪除失敗! nn"> 。 e.printStackTrace(> 。DB.close(s> 。DB.close(con> 。 catch (SQLException e> / TODO Auto-generated catch blocke.printStac
8、kTrace(> 。/* 添加類 */import java.sql.Connection。import java.sql.PreparedStatement。import java.sql.SQLException。public class Insert public void insertData(MainFrame frame, String number, String name,String dept, String grade> / public void insertData(> throws SQLException Connection con = null
9、。PreparedStatement s = null 。int grades = 0 。if (number.isEmpty(> | name.isEmpty(>> frame.text.append(" 學號或姓名不能為空 , 插入失敗! "> 。 return 。if(!grade.isEmpty(>>grades = Integer.parseInt(grade>。String str = "insert into student values (?,?,?,?>"。con = DB.getConn
10、(> 。try s = con.prepareStatement(str> 。s.setString(1, number> 。s.setString(2, name>。s.setString(3, dept>。s.setInt(4, grades> 。try s.executeUpdate(> 。frame.text.append(" 插入成功! "> 。 catch (Exception e> / TODO Auto-generated catch block frame.text.append(" 插入失敗
11、! "> 。 catch (SQLException e> DB.close(s> 。DB.close(con> 。import java.awt.Button。import java.awt.Choice。import java.awt.Color。import java.awt.FlowLayout。import java.awt.Font。import java.awt.Frame 。import java.awt.Graphics。import java.awt.Image 。import java.awt.Label 。import java.awt
12、.Menu 。import java.awt.MenuBar 。import java.awt.MenuItem 。import java.awt.Panel 。import java.awt.TextArea 。import java.awt.TextField 。import java.awt.Toolkit 。import java.awt.event.ActionEvent 。import java.awt.event.ActionListener 。import java.awt.event.WindowAdapter 。import java.awt.event.WindowEve
13、nt 。public class MainFrame extends Frame implements ActionListener */private static final long serialVersionUID = 1Lprivate MenuItem select, insert, delete, updateprivate TextField rescan, scan, number, name, dept, grade private Panel panel 。private Choice choice。private int flag = 0 。private MenuBa
14、r jmb 。TextField username, password, repassword 。Button submit, zhuce 。TextArea text 。public static void main(String args> / TODO Auto-generated method stubMainFrame frame = new MainFrame(> 。frame.frame(> 。/* 創建主窗口 */ public void frame(> setSize(400, 400> 。 setTitle(" 學生信息管理 &quo
15、t;> 。 setLocation(400, 200> 。/ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE> / pack(> 。this.addWindowListener(new WindowAdapter(> public void windowClosing(WindowEvent e> setVisible(false> 。 System.exit(-1> 。>。setResizable(false> 。 setVisible(true> 。 denglu(> 。pu
16、blic void paint(Graphics g> Image image Toolkit.getDefaultToolkit(>.getImage("src/welcom.jpg">。g.drawImage(image, 0, 20, 400, 400, this>。/* 對窗口的菜單欄標簽進行初始化并添加監聽 */ public void init(> panel = new Panel(> 。 jmb = new MenuBar(> 。 setMenuBar(jmb> 。 Menu file = new Menu(&
17、quot; 菜單 "> 。 Menu exit = new Menu(" 選項 "> 。 jmb.add(file> 。 jmb.add(exit> 。file.add(select = new MenuItem("查詢 ">> 。file.add(insert = new MenuItem("添加 ">> 。file.add(delete = new MenuItem("刪除 ">> 。file.add(update = new MenuIte
18、m("修改 ">> 。exit.add(new MenuItem(" 退出登陸 ">> 。 exit.add(new MenuItem(" 返回 ">> 。 exit.addActionListener(this> 。 select.addActionListener(this>。insert.addActionListener(this>。delete.addActionListener(this>。update.addActionListener(this>。/*
19、事件監聽 */Overridepublic void actionPerformed(ActionEvent e> / TODO Auto-generated method stubString actionCommand = e.getActionCommand(> 。boolean flags = false。if (" 登陸 ".equals(actionCommand>> flags = new User(>.isRight(this, username.getText(>.trim(>, password.getText(
20、>.trim(>> 。if (flags> init(> 。 this.removeAll(> 。if (" 注冊 ".equals(actionCommand>> addUser(> 。if (" 提交 ".equals(actionCommand>> flags = false 。flags = this.isEmpty(username.getText(>.trim(>,password.getText(>.trim(>, repassword.getText
21、(>.trim(>>if (flags> flags = false 。newflags =User(>.addUser(this,username.getText(>.trim(>, password.getText(>.trim(>> 。if(flags> this.removeAll(> 。 denglu(> 。if (" 重置 ".equals(actionCommand>> username.setText(""> 。password.setText
22、(""> 。repassword.setText(""> 。if (" 退出登陸 ".equals(actionCommand>> this.removeAll(> 。this.remove(jmb> 。denglu(> 。if (" 返回 ".equals(actionCommand>> this.removeAll(> 。 if (" 查詢 ".equals(actionCommand>> selects(> 。
23、flag = 1 。 if (" 添加 ".equals(actionCommand>> insert(> 。 flag = 2 。 if (" 刪除 ".equals(actionCommand>> delete(> 。 flag = 3 。 if (" 修改 ".equals(actionCommand>> update(> 。 flag = 4 。 if (" 確定 ".equals(actionCommand>> if (flag = 1&g
24、t; text.setText(""> 。new Select(>.select(this, scan.getText(>.trim(>, choice.getSelectedItem(>> 。 else if (flag = 2> text.setText(""> 。 new Insert(>.insertData(this, number.getText(>.trim(>, name.getText(>.trim(>, dept.getText(>.trim(>
25、, grade.getText(>.trim(>> 。 else if (flag = 3> text.setText(""> 。 new Drop(>.drop(this, scan.getText(>.trim(>,choice.getSelectedItem(>> 。Illinew Select(>.select(this, "all"> 。 else text.setText(""> 。scan.getText(>.trim(>, res
26、can.getText(>.trim(>,choice.getSelectedItem(>> 。/* 創建登錄窗口 */ public void denglu(> panel = new Panel(> 。 Font font = new Font("宋體 ", 20, 18> 。panel.setBackground(Color.gray> 。 panel.setLayout(null> 。 username = new TextField(20> 。 password = new TextField(20>
27、; 。 Label lusername = new Label("用戶名: "> 。Label lpassword = new Label("密 碼: "> 。submit = new Button(" 登陸 "> 。 zhuce = new Button(" 注冊 "> 。 lusername.setFont(font> 。 username.setFont(font> 。 lpassword.setFont(font> 。 password.setFont(font&g
28、t; 。 lusername.setBounds(90, 70, 60, 50> 。 username.setBounds(180, 80, 120, 25>。lpassword.setBounds(90, 108, 60, 50>。password.setBounds(180, 113, 120, 25> 。 submit.setBounds(180, 150, 50, 30>。zhuce.setBounds(240, 150, 50, 30> 。 panel.add(lusername> 。 panel.add(username> 。 pan
29、el.add(lpassword> 。 panel.add(password> 。 panel.add(submit> 。 panel.add(zhuce> 。 submit.addActionListener(this> 。 zhuce.addActionListener(this> 。 add(panel> 。 init(> 。 this.remove(jmb> 。/* 注冊窗口 */ public void addUser(> this.removeAll(> 。 panel = new Panel(> 。 Font
30、 font = new Font(" 宋體 ", 20, 18> 。 panel.setBackground(Color.gray> 。 panel.setLayout(null> 。 username = new TextField(20> 。 password = new TextField(20> 。 repassword = new TextField(20> 。 / username.setFont(> 。Label lusername = new Label(" 用戶名: "> 。 Label
31、lpassword = new Label(" 密 碼: "> 。 Label rlpassword = new Label(" 重 復: "> 。 submit = new Button(" 提交 "> 。 zhuce = new Button(" 重置 "> 。 lusername.setFont(font> 。 username.setFont(font> 。 lpassword.setFont(font> 。 password.setFont(font> 。
32、rlpassword.setFont(font> 。 repassword.setFont(font> 。 lusername.setBounds(90, 70, 60, 50> 。 username.setBounds(180, 80, 130, 25>。lpassword.setBounds(90, 108, 60, 50>。password.setBounds(180, 120, 130, 25>。rlpassword.setBounds(90, 152, 60, 50>。repassword.setBounds(180, 160, 130, 2
33、5> 。 submit.setBounds(180, 200, 50, 30>。zhuce.setBounds(250, 200, 50, 30> 。 panel.add(lusername> 。 panel.add(username> 。/ username.setLocation(100, 100> 。 panel.add(lpassword> 。 panel.add(password> 。panel.add(rlpassword> 。 panel.add(repassword> 。 panel.add(submit> 。
34、panel.add(zhuce> 。 submit.addActionListener(this> 。 zhuce.addActionListener(this> 。 add(panel> 。init(> 。this.remove(jmb> 。/* 創建查詢窗口 */public void selects(> this.removeAll(> 。 panel = new Panel(> 。 panel.add(new Label(" 查找的關鍵字 :">> 。 panel.setLayout(new Flow
35、Layout(FlowLayout.LEFT>> submit = new Button(" 確定 "> 。 scan = new TextField(30> 。 choice = new Choice(> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。 choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 choi
36、ce.add("all"> 。 panel.add(scan> 。 panel.add(new Label(" 查找方式 :">> 。 panel.add(choice> 。 panel.add(submit> 。 panel.add(text> 。 submit.addActionListener(this> 。 add(panel> 。 init(> 。/* 創建插入窗口 */ public void insert(> this.removeAll(> 。 submit = ne
37、w Button(" 確定 "> 。 panel = new Panel(> 。panel.setLayout(new FlowLayout(FlowLayout.LEFT>> panel.add(new Label(" 學號 :">> 。number = new TextField(40> 。 panel.add(number> 。panel.add(new Label(" 姓名 :">> 。 name = new TextField(40> 。 panel.add(
38、name> 。panel.add(new Label(" 系別 :">> 。 dept = new TextField(40>。panel.add(dept> 。panel.add(new Label(" 成績 :">> 。 grade = new TextField(40> 。 panel.add(grade> 。 panel.add(submit> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。panel.add(
39、text> 。 submit.addActionListener(this> 。 add(panel> 。init(> 。/* 創建刪除窗口 */ public void delete(> this.removeAll(> 。 panel = new Panel(> 。 panel.add(new Label(" 刪除的關鍵字 ">> 。 panel.setLayout(new FlowLayout(FlowLayout.LEFT>> submit = new Button(" 確定 "&
40、gt; 。 text = new TextArea(10, 50> 。 text.setEditable(false> 。 scan = new TextField(30> 。 choice = new Choice(> 。choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 panel.add(scan> 。 panel.add(new Label(" 刪除方式 :">> 。 p
41、anel.add(choice> 。 panel.add(submit> 。panel.add(text> 。 submit.addActionListener(this> 。 add(panel> 。init(> 。/* 創建修改窗口 */ public void update(> this.removeAll(> 。 panel = new Panel(> 。 panel.setLayout(new FlowLayout(FlowLayout.LEFT>> submit = new Button(" 確定 &quo
42、t;> 。 scan = new TextField(30> 。 rescan = new TextField(30>。choice = new Choice(> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。 choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 choice.add("grade"> 。panel
43、.add(new Label(" 要修改的學號: ">> 。panel.add(scan> 。 panel.add(new Label(" 修改后的內容: ">> 。 panel.add(rescan> 。panel.add(new Label(" 要修改的內容: ">> 。 panel.add(choice> 。panel.add(submit> 。 panel.add(text> 。 submit.addActionListener(this> 。 add(p
44、anel> 。init(> 。/* 對注冊的窗口輸入內容進行判斷是否為空 */ public boolean isEmpty(String username, String password, repassword> if (username.isEmpty(>> this.username.setText(" 用戶名不能為空 "> 。 return false 。if (password.isEmpty(>> this.password.setText(" 密碼不能為空 "> 。 return fa
45、lse 。if (password.equals(repassword> = false> this.password.setText(" 密碼不一致 "> 。 return false 。return true 。1*2 e ?aa */ 。 。public class Select public void select(MainFrame frame, String number, choice> Connection con = null 。Statement s = null 。ResultSet rs = null 。boolean fla
46、g = false 。if (number.isEmpty(> && "all".equals(choice> = false>StringStringframe.text.append("1?ux ?2? u ?a? £ ? £ ?">returncon = DB.getConn(>。String str = "select * from student where " + choice + " = '" + numberninif (&q
47、uot;all".equals(choice>> str = "select * from student" try s = con.createStatement(> rs = s.executeQuery(str>frame.text.append("?§ o?ttD?tt? 土 ett3 e ? " nn">while (rs.next(>> byte namebuffer = new byte512 byte deptbuffer = new byte512namebuffe
48、r = rs.getBytes("name">。deptbuffer = rs.getBytes("dept">。String name = new String(namebuffer> String dept = new String(deptbuffer> flag = true 。frame.text.append(rs.getString("number"> + "tt" + name + "tt""tt"+ dept + rs.getS
49、tring("grade"> + "n">。if (flag = false>(”?T' ? £ ? £ ? £ ?">DB.close(rs> 。DB.close(s> 。 DB.close(con> 。 catch (SQLException e> /* 更新類 */import java.sql.Connection 。import java.sql.SQLException 。import java.sql.Statement 。public clas
50、s Update public void update(MainFrame frame, String scan, String rescan,String choice> Connection con = null。Statement s = null 。 if(scan.isEmpty(>|rescan.isEmpty(>>frame.text.append(" 內容不能為空!,更新失敗! "> 。 return 。con = DB.getConn(> 。String sql = "update student set &q
51、uot; + choice + "='" + rescan + "' where number =" + "'" + scan + "'" if ("grade".equals(choice>> sql = "update student set " + choice + "="+ " wherenumber+ Integer.parseInt(rescan>" + "'" + scanI Hill+ "'" 。 try s = con.createStatement(> 。 s.executeUpdate(sql> 。 frame.text.append(" 修改成功! DB.close(s> 。DB.close(s> 。 catch (SQLException e> / TODO Auto-generated catch block frame.text.append(" 修改失敗!
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電子產品檢測技術專業教學標準(高等職業教育專科)2025修訂
- 2024-2025學年吉林省通化市梅河口五中高二下學期4月月考英語試題及答案
- 智能交通技術專業教學標準(高等職業教育專科)2025修訂
- 2025年中國卷巾紙巾行業市場全景分析及前景機遇研判報告
- 稅務師考試東奧課件下載
- 稅務師考試2021課件
- 2025年中國站式減壓器行業市場發展前景及發展趨勢與投資戰略研究報告
- 中國潔凈環境測試儀組合套件儀器箱行業市場調查研究及投資前景展望報告
- 智能控制器培訓課件
- 2025年中國電子書閱讀器行業市場調研分析及投資前景預測報告
- 自主招生試題及答案網
- 2025年高考江蘇卷物理真題(解析版)
- 2025年重慶市中考化學試卷真題(含標準答案)
- 2024年北京市初中學業水平考試語文試卷及答案
- 電力行業電力運行維護與故障處理知識題庫
- 2025年遼寧省高考生物試卷(含答案)
- 公司期貨交易管理制度
- 心臟康復護理
- 2025年高考二卷數學
- 科學技術普及法解讀
- 健康管理師職業資格考試試卷及答案2025年
評論
0/150
提交評論