更新时间2020-12-19 06:08:27
catch (ClassNotFoundException e1)显示有错误是什么原因,下面是原代码,如果按照eclipse的提示解决问题运行后又会显示 Unknown column 'name' in 'where clause' 有没有大神知道问题是在哪里啊(另外我自己觉得可能有问题改了却还是没效果的地方已在代码中标出)package 测试;import java.awt.Container;import java.awt.EventQueue;import java.awt.LayoutManager; import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.JPasswordField;import javax.swing.JButton;import javax.swing.JDialog; import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; //import com.itheima.mian.showlist; //import com.itheima是复制代码里带的,不知道有没有用import 测试.connectionlain; public class login {private JFrame frame;private JTextField textField;private JPasswordField passwordField;private JButton quitbt; /*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {login window = new login();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});} /*** Create the application.*/public login() {initialize();} /*** Initialize the contents of the frame.*/private void initialize() { frame = new JFrame();frame.setBounds(400, 200, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);frame.setResizable(false);frame.getContentPane().setLayout(null); JLabel j1=new JLabel("***管理员模式登陆***");j1.setBounds(120, 26, 180, 15);frame.getContentPane().add(j1); JLabel lblNewLabel = new JLabel("用户名:");lblNewLabel.setBounds(100, 56, 54, 15);frame.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("密码:");lblNewLabel_1.setBounds(100, 102, 54, 15);frame.getContentPane().add(lblNewLabel_1); textField = new JTextField();textField.setBounds(196, 53, 100, 21);frame.getContentPane().add(textField);textField.setColumns(10); passwordField = new JPasswordField();passwordField.setBounds(196, 96,100, 21);frame.getContentPane().add(passwordField); JButton btnNewButton = new JButton("登录");btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { connectionlain c = new connectionlain(); Connection conn = c.getConnect(); PreparedStatement ps = null;ResultSet rs = null; try {ps = conn.prepareStatement("select *from Lzs where name=? and password=?"); /* Lzs是我自己建立的表其中含User name和mi ma两项 name 和password是复制别人代码里的不知道是不是应该换掉 */ ps.setString(1, textField.getText());ps.setString(2, passwordField.getText()); rs = ps.executeQuery();if (rs.next()) {try {new Text(); //Text()为一个新窗口frame.removeNotify();} catch (ClassNotFoundException e1) //报错位置{// TODO Auto-generated catch blocke1.printStackTrace();} } else {JOptionPane pane = new JOptionPane("用户或密码错误"); JDialog dialog = pane.createDialog("警告"); dialog.show();} } catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} } });btnNewButton.setBounds(41, 169, 93, 23);frame.getContentPane().add(btnNewButton); JButton btnNewButton_1 = new JButton("注册");btnNewButton_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { new add();frame.removeNotify(); }});btnNewButton_1.setBounds(183, 169, 93, 23);frame.getContentPane().add(btnNewButton_1); JButton j2 = new JButton("退出");j2.setBounds(323, 169, 93, 23); frame.getContentPane().add(j2); j2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getActionCommand().equals("退出")) { System.exit(0); } } }); } private void add(JButton quitBt2) {// TODO Auto-generated method stub } private Container getContentPane() {// TODO Auto-generated method stubreturn null;} }
ClassNotFoundException是项目配置的问题
而
Unknown column 'name' in 'where clause'
的确是因为
ps = conn.prepareStatement("select *from Lzs where name=? and password=?");
这条语句,你的表中没有name及password字段,当然会报错的,要改为你的字段名
但按你描述,你的表有User name和mi ma这个有问题的,字段名不支持空格