import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class manager extends JFrame{
private JLabel jltitle,jlname,jlcomm;
private JButton jok,jcancle;
private JTextField [] jt=new JTextField [2];
private JPanel jpn,jpc,jps;
public JDesktopPane desktop = new JDesktopPane();
Container c;
private toDatabase dataToDatabase;
private String data[];
public boolean isCorrect() {
         data = new String[2];
         for(int i = 0; i < jt.length; i++) {
            if(!jt[i].getText().equals("")) {
               data[i] = jt[i].getText();
            }
            else
               return false;
         }
         return true;
      }
public manager(){
c=getContentPane();
c.add(desktop);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
jltitle=new JLabel("PERSONENL INFORMATION");
jlname=new JLabel("姓名");
jlcomm=new JLabel("联系方式");
jok=new JButton("ok");
jcancle=new JButton("Cancle");
jpn=new JPanel();
jpc=new JPanel();
jps=new JPanel();
GridLayout gd=new GridLayout(2,2,1,1);
FlowLayout fl=new FlowLayout();
jpn.setLayout(fl);
jpn.add(jltitle);
c.add(jpn,BorderLayout.NORTH);
jpc.setLayout(gd);
jpc.add(jlname);
jpc.add(jt[0]=new JTextField(15));
jpc.add(jlcomm);
jpc.add(jt[1]=new JTextField(15));
c.add(jpc,BorderLayout.CENTER);
jps.setLayout(fl);
jps.add(jok);
jps.add(jcancle);
c.add(jps,BorderLayout.SOUTH);


setSize(230,150);
setVisible(true);
}
jok.addActionListener( 该句提示错误<identifier> expected
                       new ActionListener() {
                       public void actionPerformed(ActionEvent ae) {
                                   
                                       if(isCorrect()) {                                          Thread runner = 
                                             new Thread() {
                                                public void run() {
                                         dataToDatabase = new toDatabase();
            //for checking if there is no same information in the database
 if(!dataToDatabase.isDouble("SELECT name FROM user WHERE name= '" + data[0] + "'")
dataToDatabase.update("INSERT INTO user (name,comm) VALUES ('"+
                                                                           data[0] + "','" + data[1] + "' )");
         //for setting the array of JTextField to empty
         for(int i = 0; i < jt.length; i++)
                     jt[i].setText(null);
                                  }
                        else
                                                      JOptionPane.showMessageDialog(null,"The book is in the library","Error",JOptionPane.ERROR_MESSAGE);
                          }
                           };
            runner.start();
       }
  //if there is a missing data, then display Message Dialog
           else
   JOptionPane.showMessageDialog(null,"Please, complete the information","Warning",JOptionPane.WARNING_MESSAGE);
                                    }
                                 };);
                                 
public static void main(String args[]){
manager app=new manager();
}
}
以下是我的toDatabase 类
 import java.sql.*;   public class toDatabase {
      private Connection connection = null;
      private Statement statement   = null;
      private ResultSet resultset   = null;
   
      public boolean isDouble(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
         try {
            connection = DriverManager.getConnection("jdbc:odbc:user");
            statement  = connection.createStatement();
            resultset  = statement.executeQuery(query);
            while(resultset.next()) {
               return true;
            }
            resultset.close();
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
         return false;
      }
      
      public void update(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
         try {
            connection = DriverManager.getConnection("jdbc:odbc:user");
            statement  = connection.createStatement();
            statement.executeUpdate(query);
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
      }
   }

解决方案 »

  1.   

    看你的程序是快疯了jok.addActionListener( 该句提示错误<identifier> expected
                           new ActionListener() {
                           public void actionPerformed(ActionEvent ae) {
                                       
                                           if(isCorrect()) {                                          Thread runner = 
                                                 new Thread() {
                                                    public void run() {
                                             dataToDatabase = new toDatabase();
                //for checking if there is no same information in the database
     if(!dataToDatabase.isDouble("SELECT name FROM user WHERE name= '" + data[0] + "'")
    dataToDatabase.update("INSERT INTO user (name,comm) VALUES ('"+
                                                                               data[0] + "','" + data[1] + "' )");
             //for setting the array of JTextField to empty
             for(int i = 0; i < jt.length; i++)
                         jt[i].setText(null);
                                      }
                            else
                                                          JOptionPane.showMessageDialog(null,"The book is in the library","Error",JOptionPane.ERROR_MESSAGE);
                              }
                               };
                runner.start();
           }
      //if there is a missing data, then display Message Dialog
               else
       JOptionPane.showMessageDialog(null,"Please, complete the information","Warning",JOptionPane.WARNING_MESSAGE);
                                        }
                                     };);这一段要么放在构造函数中,要么放在{}内,不能直接这样写在类里。另外,你这一段里括号都不匹配
      

  2.   

    首先确认toDatabase 类是否和manager类在同一个包中,不是的话要import
    再者你的jok.addActionListener();应放在一个函数内,还有匿名类作为参数后面不应家分号