import java.sql.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.sql.*; class Access extends Applet implements ActionListener { TextField text1=new TextField(20); Panel p1=new Panel(); Panel p2=new Panel(); static int i=3; static int j=0; Button b1=new Button("ok"); Button ex=new Button("Exit"); Label lab1=new Label("INPUT The Name"); String me[]=new String[500]; public void init() { this.setLayout(new GridLayout(2,1)); p1.setLayout(new GridLayout(3,1)); p1.add(lab1); p1.add(text1); b1.addActionListener(this); p1.add(b1); this.add(p1); p2.add(ex); ex.addActionListener(new quit()); this.add(p2); } public void actionPerformed(ActionEvent e) { me[j]=text1.getText(); this.acs(); j++; } class quit implements ActionListener { public void actionPerformed(ActionEvent e) { try { String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\javatest.MDB"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection(strurl); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs=stmt.executeQuery("select * from users"); for(int i=0;i<500;i++) { if(me[i]!=" ") {
stmt.executeUpdate("DELETE FROM users WHERE name="+me[i],i); System.out.println("delete:"+me[i]); } } } catch(Exception error1) { System.out.println(error1); } } } public void acs() { String str=text1.getText(); try { String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\javatest.MDB"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection(strurl); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); Statement stmt1=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); Statement stmt2=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs=stmt.executeQuery("select * from users"); ResultSet rs1; ResultSet rs2; System.out.println("The Name Have a"); while(rs.next()) { System.out.println(rs.getString("name")); } i++; rs1=stmt1.executeQuery("select * from users"); rs1.afterLast(); stmt1.executeUpdate("INSERT INTO users VAlUES ("+i+",'"+str+"'"+")"); rs2=stmt2.executeQuery("select * from users"); rs2.afterLast(); if(rs2.last()) { System.out.println(rs2.getString("name")); } } catch(Exception error) { System.out.println(error); } } public static void main(String args[]) { Access as=new Access(); Frame fm=new Frame("DataBase Test"); fm.add(as); as.init(); fm.setSize(300,200); fm.show(); } }
以上程序为java的数据库操作,数据库结构为有一个users表里面有两个字段(id为自动编号类型是主键,name是文本型,里面有name和name1两行数据。),这个程序成功实现了添加数据库记录可点击exit按钮无法实现删除刚刚添加的数据库记录。请问如何实现?谁能回答一下,谢谢。