车辆入库:
  import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.util.*;
public class MyEnter extends JFrame implements ActionListener{
    private JButton button1, button2;
   
    private JFrame frame;
    private Connection con;
    private Statement stmt;
    private PreparedStatement pstmt;    private JTextField jfd1, jfd2, jfd3, jfd4;
    public MyEnter() {
        frame = new JFrame("单位车辆入库");
        Container content = frame.getContentPane();
        content.setLayout(new BorderLayout());
        JPanel Jpl=new JPanel();
                JPanel Jpl2=new JPanel();
                Jpl.setLayout(new GridLayout(7,2,2,6));
                Jpl2.setLayout(new GridLayout(2,2));
        JLabel lab1 = new JLabel("车型:");
        JLabel lab2 = new JLabel("车号:");
        JLabel lab3 = new JLabel("车主:");
        JLabel lab4 = new JLabel("入库时间:");
        jfd1 = new JTextField();
        jfd1.setForeground(Color.green);
        jfd2 = new JTextField();
        jfd2.setForeground(Color.red);
        jfd3 = new JTextField();
        jfd4 = new JTextField();
        
        jfd4.addFocusListener(new FocusHandler());        button1 = new JButton("确认");
        button2 = new JButton("取消");
        button1.addActionListener(this);
        button2.addActionListener(this);        Jpl.add(lab1);
       Jpl.add(jfd1);
       Jpl.add(lab2);
        Jpl.add(jfd2);
       Jpl.add(lab3);
        Jpl.add(jfd3);
        Jpl.add(lab4);
        Jpl.add(jfd4);
        Jpl.add(button1);
        Jpl2.add(button2);
       Jpl2.add(Jpl,BorderLayout.CENTER);
                content.add(Jpl2,BorderLayout.SOUTH);
                frame.pack();        frame.setVisible(true);
        frame.setSize(500, 500);
       
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:library");
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                       ResultSet.CONCUR_READ_ONLY);
            pstmt = con.prepareStatement(
                    "insert into car(carname,carnumber,carhomename,entertime)" +
                    "values(?,?,?,?) ");        } catch (ClassNotFoundException e) {
            System.err.println(e);
        } catch (SQLException e) {
            System.err.println(e);
        }    } class FocusHandler implements FocusListener{
     
     java.util.Date today1 = new java.util.Date();
     DateFormat format = DateFormat.getDateInstance();
     String formatted1 = format.format(today1);
     public void focusGained(FocusEvent e) {         Object obj = (JTextField) e.getSource();
         if (obj == jfd4) {
             jfd4.setText(formatted1);
         }     }
     public void focusLost(FocusEvent e)
        {
        } }
     public void actionPerformed(ActionEvent e) {
         String str1, str2, str3, str4;
         
         str1 = jfd1.getText().trim();
         str2 = jfd2.getText().trim();
         str3 = jfd3.getText().trim();
         str4 = jfd4.getText().trim();
         java.sql.Date today1;         Object obj = (JButton)e.getSource();
         try{
             if (obj == button1) {
                 today1 = java.sql.Date.valueOf(str4);
                 if (str1.equals("") | str2.equals("") | str3.equals("") |
                     str4.equals("")) {
                     JOptionPane.showMessageDialog(frame,
                             "The Text can't be null!");
                     return;
                     pstmt.setString(1, str1);
                     pstmt.setString(2, str2);
                     pstmt.setString(3, str3);
                     pstmt.setDate(4, today1);
                     pstmt.executeUpdate();
                     JOptionPane.showMessageDialog(frame, "add successful!");
                 }             }
             if (obj == button2) {
                 stmt.close();
                 con.close();
                 frame.dispose();
             }
         }
      catch(SQLException sqle)
          {System.err.println(sqle.getMessage());
          }
    }public static void main(String[]args)
 {          new MyEnter();
 }
}编译出错:无法访问的语句: pstmt.setString(1,str1);

解决方案 »

  1.   

    在方法 actionPerformed  中 pstmt 既没有定义,也没有参数值传过来,请问它如何进行使用呢?
      

  2.   

    是这样的,刚刚运行出来了
    但连接数据库出错
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.text.DateFormat;
    import java.util.*;
    public class MyEnter extends JFrame implements ActionListener{
        private JButton button1, button2;
       
        private JFrame frame;
        private Connection con;
        private Statement stmt;
        private PreparedStatement pstmt;    private JTextField jfd1, jfd2, jfd3, jfd4;
        public MyEnter() {
            frame = new JFrame("单位车辆入库");
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            JPanel Jpl=new JPanel();
                    JPanel Jpl2=new JPanel();
                    Jpl.setLayout(new GridLayout(7,2,2,6));
                    Jpl2.setLayout(new GridLayout(2,2));
            JLabel lab1 = new JLabel("车型:");
            JLabel lab2 = new JLabel("车号:");
            JLabel lab3 = new JLabel("车主:");
            JLabel lab4 = new JLabel("入库时间:");
            jfd1 = new JTextField();
            jfd1.setForeground(Color.green);
            jfd2 = new JTextField();
            jfd2.setForeground(Color.red);
            jfd3 = new JTextField();
            jfd4 = new JTextField();
            
            jfd4.addFocusListener(new FocusHandler());        button1 = new JButton("确认");
            button2 = new JButton("取消");
            button1.addActionListener(this);
            button2.addActionListener(this);        Jpl.add(lab1);
           Jpl.add(jfd1);
           Jpl.add(lab2);
            Jpl.add(jfd2);
           Jpl.add(lab3);
            Jpl.add(jfd3);
            Jpl.add(lab4);
            Jpl.add(jfd4);
            Jpl2.add(button1);
            Jpl2.add(button2);
          content.add(Jpl,BorderLayout.CENTER);
                    content.add(Jpl2,BorderLayout.NORTH);
                    frame.pack();        frame.setVisible(true);
            frame.setSize(500, 500);
           
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:car");
               
                String str;   
                  str="insert into myenter(carname,carnumber,carhomename,entertime)" +
                        "values(?,?,?,?) )";
                    pstmt=con.prepareStatement(str,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                     ResultSet.CONCUR_UPDATABLE);
                    stmt=con.createStatement();        }  catch(SQLException e)
                       {                   }
                       catch(ClassNotFoundException cnfe)
                       {
                       }
        } class FocusHandler implements FocusListener{
         
         java.util.Date today1 = new java.util.Date();
         DateFormat format = DateFormat.getDateInstance();
         String formatted1 = format.format(today1);
         public void focusGained(FocusEvent e) {         Object obj = (JTextField) e.getSource();
             if (obj == jfd4) {
                 jfd4.setText(formatted1);
             }     }
         public void focusLost(FocusEvent e)
            {
            } }
         public void actionPerformed(ActionEvent e) {
             String str1, str2, str3, str4;
             
             str1 = jfd1.getText().trim();
             str2 = jfd2.getText().trim();
             str3 = jfd3.getText().trim();
             str4 = jfd4.getText().trim();
             java.sql.Date today1;         Object obj = (JButton)e.getSource();
             try{
                 if (obj == button1) {
                     today1 = java.sql.Date.valueOf(str4);
                     
                         pstmt.setString(1, str1);
                         pstmt.setString(2, str2);
                         pstmt.setString(3, str3);
                         pstmt.setDate(4, today1);
                         pstmt.executeUpdate();
                         JOptionPane.showMessageDialog(frame, "add successful!");
                     }             
                 if (obj == button2) {
                     stmt.close();
                     con.close();
                     frame.dispose();
                 }
             }
          catch(SQLException sqle)
              {System.err.println(sqle.getMessage());
              }
        }public static void main(String[]args)
     {          new MyEnter();
     }
    }
    报错:[Microsoft][ODBC Microsoft Access Driver] SQL 语句的结束位置缺少分号 (;)。
    没什么地方要加";"啊
      

  3.   

    问题找到了
                       return;
                         pstmt.setString(1, str1);
                         pstmt.setString(2, str2);
                         pstmt.setString(3, str3);
    是因为你的return语句,下面的语句无法到达。
    正确的错误是这样的,
    MyEnter.java:117: unreachable statement
                         pstmt.setString(1, str1);
                         ^
    1 error
      

  4.   

    String str;   
                  str="insert into myenter(carname,carnumber,carhomename,entertime)" +
                        "values(?,?,?,?) )";去掉一个  )
      

  5.   

    出错, 
    Thread[AWT-EventQueue-0](Suspended(exception IllegalArgumentException))
    EventDispatchThread.run() line: not available[local variables unavailable]
    其他的调用正常。.....