熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用Java Swing界面實現數據庫基本操作

2013-11-23 18:40:18  來源: Java核心技術 

  實例用Java Swing圖形化界面來對數據庫操作(增改)

  數據庫版本SQLServer

  數據庫名dxaw

  用戶名dxaw

  密碼

  表名bankAccount

  表結構id(int[自動增長])ownerName(varchar)accountValue(varchar)accountLevel(varchar)

  程序代碼

  

  import javaawteventActionEvent; import javaawteventActionListener; import javasql*; import javaawt*; import javaxswing*; public class data extends JFrame implements ActionListener{ JButton addselectdelupdate; JTable table; Object body[][]=new Object[][]; String title[]={編號姓名分數級別}; Connection conn; Statement stat; ResultSet rs; JTabbedPane tp; public data() { super(數據庫操作); thissetSize(); thissetLocation(); thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); JPanel ps=new JPanel(); add=new JButton(添加); select=new JButton(顯示); update=new JButton(更改); del=new JButton(刪除); addaddActionListener(this); selectaddActionListener(this); updateaddActionListener(this); deladdActionListener(this); psadd(add);psadd(select);psadd(update);psadd(del); table=new JTable(bodytitle); tp=new JTabbedPane(); tpadd(bankAccount表new JScrollPane(table)); thisgetContentPane()add(tpCenter); thisgetContentPane()add(psSouth); thissetVisible(true); nnection(); } public void connection(){ try { ClassforName(commicrosoftjdbcsqlserverSQLServerDriver); String url=jdbc:microsoft:sqlserver://localhost:;DatabaseName=dxaw; conn=DriverManagergetConnection(urldxaw); stat = conncreateStatement(ResultSetTYPE_SCROLL_INSENSITIVE ResultSetCONCUR_READ_ONLY); } catch (Exception ex) { } } public static void main(String[] args) { data data = new data(); } public void actionPerformed(ActionEvent e) { if(egetSource()==add) {add();} if(egetSource()==select) {select();} if(egetSource()==update) {update();} if(egetSource()==del) {del();} } public void del() { try { int row=tablegetSelectedRow(); statexecuteUpdate(delete bankAccount where accountID=+body[row][]+); JOptionPaneshowMessageDialog(null數據已成功刪除); thisselect(); } catch (SQLException ex) { } } public void update() { try { int row=tablegetSelectedRow(); JTextField t[]=new JTextField[]; t[]=new JTextField(輸入姓名:); t[]setEditable(false); t[]=new JTextField(); t[]=new JTextField(輸入分數:); t[]setEditable(false); t[]=new JTextField(); t[]=new JTextField(輸入級別:); t[]setEditable(false); t[]=new JTextField(); String but[]={確定取消}; int go=JOptionPaneshowOptionDialog( nullt插入信息JOptionPaneYES_OPTION JOptionPaneINFORMATION_MESSAGEnullbutbut[]); if(go==){ String ownerName=new String(t[]getText()getBytes(ISO) GBK); String accountValue=t[]getText(); int accountLevel=IntegerparseInt(t[]getText()); statexecuteUpdate(update bankAccount set ownerName=+ownerName+ accountValue=+accountValue+accountLevel=+accountLevel+ where accountID=+body[row][]+); JOptionPaneshowMessageDialog(null修改數據成功); thisselect(); } } catch (Exception ex) { } } public void select() { try { for(int x=;x<bodylength;x++){ body[x][]=null; body[x][]=null; body[x][]=null; body[x][]=null; } int i=; rs=statexecuteQuery(select * from bankAccount); while(rsnext()){ body[i][]=rsgetInt(); body[i][]=rsgetString(); body[i][]=rsgetString(); body[i][]=rsgetInt(); i=i+; } thisrepaint(); } catch (SQLException ex) { } } private void add() { try { JTextField t[]=new JTextField[]; t[]=new JTextField(輸入姓名:); t[]setEditable(false); t[]=new JTextField(); t[]=new JTextField(輸入分數:); t[]setEditable(false); t[]=new JTextField(); t[]=new JTextField(輸入級別:); t[]setEditable(false); t[]=new JTextField(); String but[]={確定取消}; int go=JOptionPaneshowOptionDialog(nullt插入信息 JOptionPaneYES_OPTIONJOptionPaneINFORMATION_MESSAGEnullbutbut[]); if(go==){ try{ String ownerName=new String(t[]getText()getBytes(ISO)GBK); String accountValue=t[]getText(); int accountLevel=IntegerparseInt(t[]getText()); statexecuteUpdate(insert into bankAccount (ownerNameaccountValueaccountLevel) values (+ownerName++accountValue++accountLevel+)); JOptionPaneshowMessageDialog(null數據已成功插入!); }catch(Exception ee){ JOptionPaneshowMessageDialog(null插入數據錯誤!); } } } catch (Exception ex) { } } }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25553.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.