实例用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_INSENSITIVEResultSetCONCUR_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_OPTIONJOptionPaneINFORMATION_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) { } }}