如何解决Java无法连接数据库
大家!我正在做一个需要连接数据库(MySQL 工作台)的项目(Java Eclipse)。 但我面临的问题是登录页面。我已经建立了一个帐户和密码数据库,我使用 JFrame 连接该数据库以在登录页面中使用。我在数据库中建立了 student_ID = 'S0761129' 和 student_Password = 'S0761129' 。但是,我在 JFrame 中输入了相同的内容。但系统显示“帐户和密码不正确”。其实我已经上网了,按照步骤操作。不幸的是,我无法运行该程序。希望大家给我一些建设性的建议。这是完整的代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LoginPage extends JFrame {
private JPanel contentPane;
private JTextField account;
private JTextField password;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginPage frame = new LoginPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LoginPage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,297,300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblId = new JLabel("ID");
lblId.setFont(new Font("新細明體",Font.PLAIN,18));
lblId.setBounds(41,84,49,37);
contentPane.add(lblId);
account = new JTextField();
account.setBounds(101,92,121,21);
contentPane.add(account);
account.setColumns(10);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("新細明體",18));
lblPassword.setBounds(26,134,136,20);
contentPane.add(lblPassword);
password = new JTextField();
password.setBounds(99,137,120,21);
contentPane.add(password);
password.setColumns(10);
JLabel lblLoginSystem = new JLabel("Login System");
lblLoginSystem.setFont(new Font("標楷體",30));
lblLoginSystem.setBounds(56,22,239,52);
contentPane.add(lblLoginSystem);
JButton Login = new JButton("Login");
Login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_project","root","");
Statement stmt = con.createStatement();
String sql = "Select * from student_info where ID = 0 and Student_ID='" + account.getText() + "' and Student_Password = '" + password.toString() + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
JOptionPane.showMessageDialog(null,"Login Sucessfully...");
else
JOptionPane.showMessageDialog(null,"Incorrect Account and Password...");
con.close();
} catch (Exception e) {
System.out.print(e);
}
}
});
Login.setBounds(56,190,76,23);
contentPane.add(Login);
JButton Reset = new JButton("Reset");
Reset.setBounds(142,86,23);
contentPane.add(Reset);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。