getText()无法在java中工作

时间:2014-01-31 12:42:21

标签: java swing jdbc odbc

这是我的代码,getText()没有返回从JTextField获取文本。我的数据库只有两个colomns MovieName& AirDate&系统DSN为movieDSN

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MovieReminder implements ActionListener{
    //Public members of class
    JFrame frame= new JFrame("Add Movie");
    JPanel moviePane=new JPanel();
    JButton button=new JButton("Add Movie Time");
        JTextField movieName;
        JTextField airDate= new JTextField(15);
        Statement st;
        Connection con;
        String url;
        String sql;
                         String tmpMovieName;
                 String tmpairDate=airDate.getText();

    //GUI method of class making it public to access out side from class as well
    public void initGUI(){
        //adding action listener to our button & its class will be over-ridden at the end
        button.addActionListener(this);
        moviePane.add(new JLabel("Movie Name"));
                movieName=new JTextField(15);
                moviePane.add(movieName);
                tmpMovieName=movieName.getText();

                moviePane.add(new JLabel ("Air Date"));
                moviePane.add(airDate);
                moviePane.add(button);
                frame.add(moviePane);
        frame.setSize(400,400);
        frame.setVisible(true);
    }

        //sql method
        public void addMovie(){
            try{
                //loading Driver
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                //Making Connection
                String url="jdbc:odbc:movieDSN";
                 con=DriverManager.getConnection(url);
                //Creating Statement
                 st=con.createStatement();
                //sql Query to insert record

                 //sql="INSERT INTO movieTime (MovieName,AirDate) VALUES ( '"+movieName.getText()+"','"+airDate.getText()+"' ) ";
                 sql="INSERT INTO movieTime (MovieName,AirDate) VALUES ( '"+tmpMovieName+"','"+tmpairDate+"' ) ";

            }catch(Exception sqlex){
            System.out.println(sqlex);
            }
        }



    //constructor of main class
    MovieReminder(){
        initGUI();
                addMovie();
    }
    //overriding action performed method for our functions
    public void actionPerformed(ActionEvent e){
            try {
                st.executeUpdate(sql);
            } catch (SQLException ex) {
                System.out.println(ex);
            }
            JOptionPane.showMessageDialog(null,"Value Added "+tmpMovieName);
    }

    public static void main(String args[]){
    MovieReminder myObject=new MovieReminder();
    }


}//end movieReminder class

3 个答案:

答案 0 :(得分:1)

据我所见 你没有初始化MovieName。

movieName=new JTextField(15);
moviePane.add(movieName);
tmpMovieName=movieName.getText();

MovieName是一个空的JTextField

你应该尝试设置

tmpMovieName=movieName.getText();

在你的actionperformed方法

在调用sql语句之前执行此操作 而且 从jtextfield获取文本时,应该更新你的sql语句

答案 1 :(得分:1)

您已将代码放入错误的位置,请尝试将代码放入actionPerformed

public void actionPerformed(ActionEvent e){
        try {
            tmpMovieName=movieName.getText();    //Put your code here
            System.out.println("tmpMovieName:"+tmpMovieName);
            st.executeUpdate(sql);
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        JOptionPane.showMessageDialog(null,"Value Added "+tmpMovieName);
}

您应该在动作事件中构建查询。并尝试使用PreparedStatements来安排sql injection

答案 2 :(得分:1)

它是illegal forward reference ..你不能在声明部分使用gettext() ..

所以这样写。,

 public void addMovie()
 {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url="jdbc:odbc:movieDSN";
            con=DriverManager.getConnection(url);
            st=con.createStatement();
            String tmpairDate=airDate.getText();
            sql="INSERT INTO movieTime (MovieName,AirDate) VALUES ( '"+tmpMovieName+"','"+tmpairDate+"' ) ";

        }catch(Exception sqlex)
        {
            System.out.println(sqlex);
        }
 }

...谢谢