【发布时间】:2014-02-24 03:57:49
【问题描述】:
这是我的代码,getText() 没有返回从 JTextField 获取文本。我的数据库只有两个列,MovieName & AirDate & System 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
【问题讨论】:
-
双重否定......无缘无故不工作......对程序员=工作:)
-
@JoshEngelsma 这不是双重否定,因为工作和原因是单独的上下文,它们对它们施加了否定,即 !working && !reason
-
如果这个程序不工作,那么显然它不工作是有原因的。没有理由不工作,因为如果没有理由不工作,那么它就会工作