第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JAVA中圖形面板顯示數(shù)據(jù)庫(kù)中的信息

JAVA中圖形面板顯示數(shù)據(jù)庫(kù)中的信息

橙子lovesun 2016-06-05 23:14:23
用JAVA編寫代碼,數(shù)據(jù)庫(kù)中的學(xué)生信息表在圖形界面中顯示,圖形界面做出來了,可是點(diǎn)擊顯示,卻無法在界面中顯示數(shù)據(jù)庫(kù)中的信息,大神們,可以幫忙看看嗎?做出的圖形界面:當(dāng)點(diǎn)擊“顯示”,下圖數(shù)據(jù)庫(kù)中的信息就在上面界面中顯示出來圖形界面沒問題,可是不能講以上信息顯示,大神們可以幫我看看嗎?代碼:import?java.awt.event.ActionEvent; import?java.awt.event.ActionListener; import?java.sql.Connection; import?java.sql.DriverManager; import?java.sql.ResultSet; import?java.sql.SQLException; import?java.sql.Statement; import?java.util.ArrayList; import?java.util.List; import?javax.swing.JButton; import?javax.swing.JFrame; import?javax.swing.JMenu; import?javax.swing.JMenuBar; import?javax.swing.JOptionPane; import?javax.swing.JPanel; import?javax.swing.JScrollPane; import?javax.swing.JTable; import?javax.swing.table.DefaultTableModel; import?javax.swing.table.TableModel; public?class?SM?extends?JFrame?implements?ActionListener{//?學(xué)生信息管理 ??int?width=600;?//窗體的寬度 ??????int?height=400;//高度 ??????JTable?sTable;// ??????TableModel?model;//表模型 ??????JScrollPane?scroll;//帶滾動(dòng)條的面板 ??????Object[][]?playerInfo;//存放表格中的數(shù)據(jù) ??????Object[]?columnNames;//定義表格中的列名 ?? JPanel?p1?=?new?JPanel(); ?? JPanel?p?=?new?JPanel(); ??????Connection?con?=?null;//?conn獲取鏈接 ?? Statement?stmt?=?null;//?通過鏈接創(chuàng)建Statement對(duì)象stmt,在通過stmt對(duì)象執(zhí)行sql語句 ?? ResultSet?rs?=?null;//?定義rs接收executeQuery()方法返回的ResultSet對(duì)象,查詢的數(shù)據(jù)就存放在rs對(duì)象中 ??????//首先創(chuàng)建菜單欄 ??????JMenuBar?bar?=?new?JMenuBar(); ??????//創(chuàng)建文件菜單 ??????JButton?btnAdd?=?new?JButton("增加"); ?? JButton?btnDelete?=?new?JButton("刪除"); ?? JButton?btnAlter?=?new?JButton("修改"); ?? JButton?btnSearch?=?new?JButton("查詢"); ?? JButton?btnDisplay?=?new?JButton("顯示"); ?? JButton?back?=?new?JButton?("返回上一級(jí)"); ??????SM(){//構(gòu)造函數(shù) ???? ??super("學(xué)生信息管理系統(tǒng)");? ??????????this.setSize(width,?height); ???????? ?????? init(); ??? this.setVisible(true);?//?顯示窗體 ??? this.setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗體的關(guān)閉方式,點(diǎn)擊小叉退出應(yīng)用程序 ??????} ?????? ??????public?void?init()?{ ???? ??add("South",?p); ???? this.add("Center",?p1); ???? ??//菜單欄上添加菜單 ???? ??bar.add(btnDisplay); ???? ??bar.add(btnAdd); ???? ??bar.add(btnDelete); ???? ??bar.add(btnAlter); ???? ??bar.add(btnSearch); ???? ??bar.add(back); ??? ???//給窗體添加菜單欄 ??? ???this.setJMenuBar(bar); ??? ???//設(shè)置窗體的位置 ??? ??int?screen_width?=?(int)this.getToolkit().getScreenSize().getWidth();?//得到屏幕的寬度 ???int?screen_height?=?(int)this.getToolkit().getScreenSize().getHeight();?//得到屏幕的高度 ???this.setLocation((screen_width-width)/2,?(screen_height-height)/2); ??this.setResizable(false);//true,則使?窗體大小可變,否則大小不可變 ???this.setVisible(true);//true,則使??窗體?可見,否則隱藏?窗體 ?//為組件注冊(cè)事件監(jiān)聽器 btnAdd.addActionListener(this); btnDelete.addActionListener(this); btnAlter.addActionListener(this); btnSearch.addActionListener(this); btnDisplay.addActionListener(this); back.addActionListener(this); ??????} ??????public?void?connDB()?{?//?連接數(shù)據(jù)庫(kù) ?? try?{ ?? Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ?? }?catch?(ClassNotFoundException?e)?{ ?? e.printStackTrace(); ?? } ?? try?{ ?? con?=?DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=DBstudent",?"sa", ?? "1234567890"); ?? stmt?=?con.createStatement(); ?? }?catch?(SQLException?e)?{ ?? e.printStackTrace(); ?? } ?? } ?? public?void?closeDB()?//?關(guān)閉連接 ?? { ?? try?{ ?? stmt.close(); ?? con.close(); ?? }?catch?(SQLException?e)?{ ?? e.printStackTrace(); ?? } ?? } ?? /* ?*?顯示所有學(xué)生的信息所以定義一個(gè)ModelST類的集合List<ModelST>來接收返回值:所有信息 ?*/ ?? public?void?dispose()?{ int?j=0; int?i=0;//記錄數(shù)據(jù)庫(kù)中有好多組信息 String[]?columnNames?=?{?"學(xué)號(hào)",?"姓名",?"年齡",?"性別",?"院系"?}; this.connDB();//?連接數(shù)據(jù)庫(kù) /* ?*?調(diào)用stmt對(duì)象的executeQuery()方法,查詢數(shù)據(jù)庫(kù)的信息 ?*?executeQuery()方法的返回只是ResultSet對(duì)象,定義rs接收返回的ResultSet對(duì)象?查詢的數(shù)據(jù)就存放在rs對(duì)象中 ?*/ try?{ ?rs?=?stmt.executeQuery("select?*?from?student?order?by?sno"); List<ModelST>?gs?=?new?ArrayList<ModelST>(); ModelST?g?=?null; /* ?*?rs對(duì)象的next()方法獲取數(shù)據(jù),若返回true證明對(duì)象中有數(shù)據(jù)?通過while循環(huán)將對(duì)象中的數(shù)據(jù)打印出啦。 ?*/ while?(rs.next())?{ g?=?new?ModelST(); g.setSno(rs.getInt("sno")); g.setDept(rs.getString("sd")); g.setName(rs.getString("sn")); g.setAge(rs.getInt("sa")); g.setSex(rs.getString("ss")); gs.add(g); i++; } playerInfo?=?new?Object[i][5]; for?(ModelST?go?:?gs)?{ playerInfo[j][0]?=?g.getSno(); playerInfo[j][1]?=?g.getName(); playerInfo[j][2]?=g.getAge(); playerInfo[j][3]?=?g.getSex(); playerInfo[j][4]?=?g.getDept(); j++; } //初始化表模型,利用子類DefaultTableModel生成TableModel接口對(duì)象 model=new?DefaultTableModel(playerInfo,?columnNames); sTable?=?new?JTable(model);//初始化JTable //初始化帶滾動(dòng)條的面板 scroll?=?new?JScrollPane(); scroll.getViewport().add(sTable); this.getContentPane().add(scroll); this.closeDB(); }catch?(SQLException?e)?{ e.printStackTrace(); } } public?void?actionPerformed(ActionEvent?e)?{ if?(e.getActionCommand()?==?"顯示")?{ /*JOptionPane.showConfirmDialog(null,?"進(jìn)入學(xué)生界面",?"提示信息",?JOptionPane.DEFAULT_OPTION, JOptionPane.OK_OPTION); */ this.dispose(); }if?(e.getActionCommand()?==?"返回上一級(jí)")?{ int?result?= JOptionPane.showConfirmDialog(null,?"您真的要退出系統(tǒng)嗎?",?"提示信息",?JOptionPane.OK_CANCEL_OPTION); ???if(result==JOptionPane.OK_OPTION) ???{ ???System.exit(0);?//退出系統(tǒng) ???} ???else ???{ ???return; ???} } } }
查看完整描述

1 回答

?
styledboy

TA貢獻(xiàn)5條經(jīng)驗(yàn) 獲得超1個(gè)贊

jdbc

查看完整回答
反對(duì) 回復(fù) 2016-06-09
  • 1 回答
  • 4 關(guān)注
  • 4855 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)