我正在制作一个Java程序,它会在登录提示后将您登录到GUI屏幕,并将您带入学生框架并登录。它连接到SQLite Manager数据库,在该数据库中,它为登录到某些JTextField的用户提取数据。我连接到数据库(我确定它正在连接),但它不会显示数据库中的数据。
这是我的登录代码:
public class LoginC {
private JFrame frame; //Frame name frame
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginC window = new LoginC();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connection=null;
private JTextField textField1;
private JPasswordField passwordField;
/**
* Create the application.
*/
public LoginC() {
initialize();
connection=SqliteConnection.dbConnector();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUserName = new JLabel("User Name");
lblUserName.setFont(new Font("Times New Roman", Font.BOLD, 15));
lblUserName.setBounds(134, 82, 84, 23);
frame.getContentPane().add(lblUserName);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Times New Roman", Font.BOLD, 15));
lblPassword.setBounds(134, 125, 84, 14);
frame.getContentPane().add(lblPassword);
textField1 = new JTextField();
textField1.setBounds(244, 82, 147, 23);
frame.getContentPane().add(textField1);
textField1.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(244, 121, 147, 23);
frame.getContentPane().add(passwordField);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String query
= " select * from Login where USERNAME=? and PASSWORD=?";
//String query="select * from LoginTb as L1,LoginTb as L2 where L1.USERNAME=L2.USERNAME and L1.PASSWORD=L2.PASSWORD; ";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1,textField1.getText() );
pst.setString(2,passwordField.getText() );
ResultSet rs=pst.executeQuery(); // variable object r s to keep track of result
int count =0;
while (rs.next())
{
count=count+1;
}
if (count ==1)
{
JOptionPane.showMessageDialog(null, "USERNAME and PASSWORD is correct");
frame.dispose ();
student ts = new student(); // import student resource
ts.setVisible(true);
}
else if (count>1)
{
JOptionPane.showMessageDialog(null, "Duplicated USERNAME and PASSWORD is invalid");
}
else
{
JOptionPane.showMessageDialog(null, "USERNAME and PASSWORD is not correct Try again...");
}
rs.close(); // close connectors
pst.close();
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
});
btnLogin.setFont(new Font("Times New Roman", Font.BOLD, 17));
btnLogin.setBounds(198, 168, 89, 23);
frame.getContentPane().add(btnLogin);
}}
这是学生的框架:
public class student extends JFrame {
private JPanel contentPane;
private JPanel contentPane1;
private JTextField textField;
private JTextField textField_3;
private JTextField textField_2;
Connection connection=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
student frame = new student();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public student() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblstudent = new JLabel("Student Screen");
lblstudent.setFont(new Font("Times New Roman", Font.BOLD, 18));
lblstudent.setBounds(142, 10, 150, 43);
contentPane.add(lblstudent);
JButton btnNewButton = new JButton("List Info");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Info();
setVisible(true);
}
});
}
protected void Info() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 585, 387);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Name");
lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel.setBounds(97, 11, 46, 14);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(20, 30, 199, 20);
contentPane.add(textField);
textField.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(242, 30, 86, 20);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(338, 30, 86, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Grade Level");
lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel_1.setBounds(242, 11, 76, 14);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Reading Level");
lblNewLabel_2.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel_2.setBounds(338, 11, 93, 14);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("Current Book");
lblNewLabel_3.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel_3.setBounds(459, 11, 100, 14);
contentPane.add(lblNewLabel_3);
try{
Class.forName("org.sqlite.JDBC");
String path=student.class.getResource("Booklander.sqlite").getPath();
Connection connection=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gozie\\Desktop\\Database\\Booklander.sqlite");
String SQL="select firstName,GradeLevel,ReadingLevel from Student,Login where firstName=? AND GradeLevel=? AND ReadingLevel=?";
Statement statement=connection.createStatement();
ResultSet rs=statement.executeQuery(SQL);
while(rs.next())
{
String FN= rs.getString("firstName");
int GL = Integer.parseInt("GradeLevel");
int RL= Integer.parseInt("ReadingLevel");
textField.setText(FN);
System.out.println(""+FN);
textField_3.setText(String.valueOf(GL));
System.out.println(GL);
textField_2.setText(String.valueOf(RL));
}
connection.close();
statement.close();
}
catch(Exception e){System.out.println("Error");
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
此外,还有一些来自数据库的图片,以备您需要时使用。 http://i.imgur.com/kLtxfXh.png
http://i.imgur.com/eZ9uatX.png
我应该使用方法返回3个值吗?或者确实使用了错误的代码?
答案 0 :(得分:0)
创建SQL查询时,请务必删除不必要的空格字符串。由于使用了字母数字,因此大多数登录名和密码都是数据库表中的STRING数据类型,因此您的查询应该以这种方式处理数据,例如,字符串数据类型应该包含在撇号(')中。
String query = "SELECT * FROM Login where USERNAME = '" + suppliedUserNameVariable +
"' AND PASSSWORD = '" + suppliedPasswordVariable + "';";
您是否注意到SQL Query字符串中的撇号?为清楚起见,尝试使用实际变量将提供的数据传递给SQL查询字符串。
您可能还想确保正确处理lettercase。