如何根据条件从MySQL数据库中读取数值?

huangapple go评论112阅读模式
英文:

How to read value from mysql database based on condition?

问题

我正在进行一个Java项目,允许用户根据他们在帐户创建时选择的角色登录为管理员或用户。我已经成功编写了代码,在我的应用程序的登录页面上检查他们的用户名和密码是否正确,方法是检查这些值是否存在于数据库中。然而,我想让用户以管理员或用户的身份登录,但我不知道如何实现。我考虑如果我可以从数据库中读取他们在帐户创建时选择的角色(用户或管理员),然后可以基于这个值编写一个条件语句。例如,如果(usertype == 'admin'){ // 转到管理员页面} 但是usertype是数据库中的一列,我不知道如何读取它。下面是我的代码,这是一个在按下登录按钮时激活的函数。如果你知道解决方案,请告诉我,谢谢。

  1. private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
  2. Connection con;
  3. PreparedStatement pst;
  4. ResultSet rs;
  5. try {
  6. String query = "SELECT * FROM `accounts` WHERE username=? and password=?"; // and usertype=?
  7. con = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
  8. pst = con.prepareStatement(query);
  9. pst.setString(1, txtUsername.getText());
  10. pst.setString(2, txtPassword.getText());
  11. rs = pst.executeQuery();
  12. if (rs.next()) {
  13. JOptionPane.showMessageDialog(this, "Login is successful as admin");
  14. mainpage admin = new mainpage();
  15. admin.setVisible(true);
  16. dispose();
  17. } else {
  18. JOptionPane.showMessageDialog(this, "Incorrect username or password");
  19. /*usermainpage user = new usermainpage();
  20. user.setVisible(true);
  21. dispose();*/
  22. }
  23. } catch (Exception ex) {
  24. JOptionPane.showMessageDialog(this, ex.getMessage());
  25. }
  26. }

这是我的数据库中的列的图片:
如何根据条件从MySQL数据库中读取数值?

  1. <details>
  2. <summary>英文:</summary>
  3. I am doing a Java project to allow users to either log in as an admin or user based on what they selected from their account creation. I managed to do a code to check if their username and password were correct in my login page of the application by checking if those values are present in the database, however, I want the user to login as either an admin or user but I don&#39;t know how to do that. Im thinking if I can read what they selected as (either user or admin) from their account creation in the database (refer below for picture of database columns), then I am able to do an if statement based on that. For example if(usertype == &#39;admin&#39;){ // then go to admin page} But usertype is a column in the database which I don&#39;t know how to read.I have my code below as well which is function that activates whenever the login button is pressed.If you know of a solution, let me know thank you.
  4. private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
  5. Connection con;
  6. PreparedStatement pst;
  7. ResultSet rs;
  8. try{
  9. String query = &quot;SELECT * FROM `accounts` WHERE username=? and password=?&quot;; // and usertype=?
  10. con = DriverManager.getConnection(&quot;jdbc:mysql://localhost/restock&quot;, &quot;root&quot;, &quot;password&quot;);
  11. pst = con.prepareStatement(query);
  12. pst.setString(1, txtUsername.getText());
  13. pst.setString(2, txtPassword.getText());
  14. rs = pst.executeQuery();
  15. if(rs.next()){
  16. JOptionPane.showMessageDialog(this, &quot;Login is successful as admin&quot;);
  17. mainpage admin = new mainpage();
  18. admin.setVisible(true);
  19. dispose();
  20. }
  21. else{
  22. JOptionPane.showMessageDialog(this, &quot;Incorrect username or password&quot;);
  23. /*usermainpage user = new usermainpage();
  24. user.setVisible(true);
  25. dispose();*/
  26. }
  27. } catch(Exception ex){
  28. JOptionPane.showMessageDialog(this, ex.getMessage());
  29. }
  30. }
  31. This is a picture of the columns that are in my database
  32. [![enter image description here][1]][1]
  33. [1]: https://i.stack.imgur.com/RqV5m.png
  34. </details>
  35. # 答案1
  36. **得分**: 1
  37. 由于你正在执行SELECT *,你应该能够在你的Java代码中通过以下方式访问usertype:
  38. ```java
  39. if(rs.next()){
  40. String userType = rs.getString("usertype");
  41. if(userType.equals("admin")){
  42. // 这里是你的管理员代码
  43. }else{
  44. // 这里是你的用户代码
  45. }
  46. }
英文:

Since you're doing a SELECT *, you should be able to access the usertype in your java code with something like

  1. if(rs.next()){
  2. String userType = rs.getString(&quot;usertype&quot;);
  3. if(userType.equals(&quot;admin&quot;)){
  4. // your admin code here
  5. }else{
  6. // your user code here
  7. }
  8. }

huangapple
  • 本文由 发表于 2020年8月6日 23:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63287310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定