package crs;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;public class LoginDialogDemo extends JFrame {
JButton button = new JButton("Click Me");
JPanel panel = new JPanel(new FlowLayout()); public LoginDialogDemo() {
final JFrame frame = this;
this.getContentPane().add(panel,BorderLayout.SOUTH);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showLoginDialog(frame);
}
});
this.setSize(300,200);
this.setTitle("显示登陆对话框");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
} void showLoginDialog(JFrame frame){
JPanel p = new JPanel(new GridLayout(0,1));
JTextField tfUserName = new JTextField();
JPasswordField tfPassword = new JPasswordField();
tfPassword.setEchoChar('&');
p.add(new JLabel("Username: "));
p.add(tfUserName);
p.add(new JLabel("Password: "));
p.add(tfPassword);
String[] str=new String[1];
if (JOptionPane.showConfirmDialog(frame // may want to pass your application frame here
,p
,"Login"
,JOptionPane.OK_CANCEL_OPTION
,JOptionPane.PLAIN_MESSAGE
) == JOptionPane.OK_OPTION) {
//System.out.println("User Name:"+tfUserName.getText());
//System.out.println("Password:" + new String(tfPassword.getPassword()));
String data="jdbc:odbc:login";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(data);
Statement st=conn.createStatement();
/*PreparedStatement pre=conn.prepareStatement("INSERT INTO Stocks VALUES(?,?,?,?,?,?,?,?)");
pre.setString(1, new String(tfUserName.getText()));
pre.setString(2, new String(tfPassword.getPassword()));*/
//pre.executeUpdate();
int rl=st.executeUpdate("INSERT INTO login value(tfUserName.getText(),tfPassword.getPassword())");
conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(ClassNotFoundException ev)
{
System.out.println(ev.getMessage());
}
}
}
请解释一下错在哪里?谢谢了。
运行正常。只是我想吧输入的用户和密码导入到数据库里面去。
就是导入不了。不知道为什么。我试过很多次了。。
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;public class LoginDialogDemo extends JFrame {
JButton button = new JButton("Click Me");
JPanel panel = new JPanel(new FlowLayout()); public LoginDialogDemo() {
final JFrame frame = this;
this.getContentPane().add(panel,BorderLayout.SOUTH);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showLoginDialog(frame);
}
});
this.setSize(300,200);
this.setTitle("显示登陆对话框");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
} void showLoginDialog(JFrame frame){
JPanel p = new JPanel(new GridLayout(0,1));
JTextField tfUserName = new JTextField();
JPasswordField tfPassword = new JPasswordField();
tfPassword.setEchoChar('&');
p.add(new JLabel("Username: "));
p.add(tfUserName);
p.add(new JLabel("Password: "));
p.add(tfPassword);
String[] str=new String[1];
if (JOptionPane.showConfirmDialog(frame // may want to pass your application frame here
,p
,"Login"
,JOptionPane.OK_CANCEL_OPTION
,JOptionPane.PLAIN_MESSAGE
) == JOptionPane.OK_OPTION) {
//System.out.println("User Name:"+tfUserName.getText());
//System.out.println("Password:" + new String(tfPassword.getPassword()));
String data="jdbc:odbc:login";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(data);
Statement st=conn.createStatement();
/*PreparedStatement pre=conn.prepareStatement("INSERT INTO Stocks VALUES(?,?,?,?,?,?,?,?)");
pre.setString(1, new String(tfUserName.getText()));
pre.setString(2, new String(tfPassword.getPassword()));*/
//pre.executeUpdate();
int rl=st.executeUpdate("INSERT INTO login value(tfUserName.getText(),tfPassword.getPassword())");
conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(ClassNotFoundException ev)
{
System.out.println(ev.getMessage());
}
}
}
请解释一下错在哪里?谢谢了。
运行正常。只是我想吧输入的用户和密码导入到数据库里面去。
就是导入不了。不知道为什么。我试过很多次了。。
不过最好设多一个主键userId
字符值外面要加 ''
st.executeUpdate("INSERT INTO login value('"+
tfUserName.getText()+"','"+tfPassword.getPassword()+"')");以后测试的时候把SQL字符串 输出看看
System.out.println(sql);
int rl=st.executeUpdate(sql);
讲点个人经验,catch中测试的时候我一般用e.printStackTrace();这样能看出哪一句开始错的,
一般出了错不要急,先看出什么错,能理解的,最好办,不能理解的网上找.
一般不能运行的把sql语句打出来看,有时候你一不小心sql语句就出错了,打出来的sql语句在数据库里运行,不能运行那是语句错.
像你这个就是sql错了tfUserName.getTest()是一个方法,你放在引号里就成了sql的一部分,value少个s如果你用e.printStackTrace()或者打印sql语句,你都可以很快发现错误
你这样的话,就直接把你调方法的语句给传进去了。