《汽车销售管理系统连接数据库》由会员分享,可在线阅读,更多相关《汽车销售管理系统连接数据库(3页珍藏版)》请在金锄头文库上搜索。
1、4.1 连接数据库连接数据库主类主类 qcxs.javapublic class qcxs public static void main(String args) ModifyRecord m=new ModifyRecord(“修改记录对话框“); 窗体窗体 ModifyRecord.javaimport javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; import java.sql.*; public class ModifyRecord extends JDialog implements Ac
2、tionListenerJTextField inputNumber; Object name=“车型代号“,“车辆类型“,“汽车型号“,“销售价“,“汽车品牌“,“汽车配置“; Object a=new Object16; JTable table; JButton enterModify; Connection con; Statement sql; ResultSet rs; String num; JPanel pSouth,pNorth; ModifyRecord(String title) setTitle(title); inputNumber=new JTextField(20
3、); table=new JTable(a,name); enterModify=new JButton(“更新记录“); pNorth=new JPanel(); pSouth=new JPanel(); pNorth.add(new JLabel(“车型代号(回车确认)“); pNorth.add(inputNumber); add(pNorth,BorderLayout.NORTH);add(new JScrollPane(table),BorderLayout.CENTER); pSouth.add(enterModify); add(pSouth,BorderLayout.SOUTH
4、); setBounds(100,100,1070,150);inputNumber.addActionListener(this); enterModify.addActionListener(this); setVisible(true); public void actionPerformed(ActionEvent e) if(e.getSource()=inputNumber) try num=inputNumber.getText().trim(); con=DriverManager.getConnection(“jdbc:odbc:yy“,“,“); sql=con.creat
5、eStatement(); rs=sql.executeQuery(“SELECT* FROM QC WHERE QNO=“+num+“); boolean boo=rs.next(); if(boo=false) JOptionPane.showMessageDialog(this, “没有这个车型“,“提示 “,JOptionPane.WARNING_MESSAGE); else a00=rs.getString(1); a01=rs.getString(2); a02=rs.getString(3); a03=rs.getString(6); a04=rs.getString(7); a
6、05=rs.getString(8); table.repaint(); con.close(); catch(SQLException ee) System.out.println(ee); if(e.getSource()=enterModify) try con=DriverManager.getConnection(“jdbc:odbc:yy“,“,“); sql=con.createStatement(); sql.executeUpdate(“UPDATE QC SET QNO=“+a00+“,QLX=“+a01+“,QXH=“+a02+“,QXSJ=“+a03+“,QPP=“+a04+ “,QPZ=“+a05+“WHERE QNO=“+num+“); JOptionPane.showConfirmDialog(this, “更新成功“,“成功 “,JOptionPane.PLAIN_MESSAGE); con.close(); catch(SQLException ee)JOptionPane.showConfirmDialog(this, “更新失败“+ee,“失败 “,JOptionPane.ERROR_MESSAGE);