如何将MySQL表的列值检索到代码中?

时间:2017-03-15 10:14:13

标签: java mysql jdbc

public String User;
Model.JDBC j=new JDBC();
/**
 * Creates new form Home_Page
 */
public Home_Page(String eid) 
{
    initComponents();

    User=eid;
    Toolkit t = Toolkit.getDefaultToolkit();
    Dimension d = t.getScreenSize();
    int w = (int) d.getWidth();
    int h = (int) d.getHeight()-40;
    setBounds(0, 0, w, h);
    jLabel2.setText(new Controler.getDate().date());
    time();
    jLabel3.setText(eid);
    ResultSet rs;

    try 
    {

      rs = j.getData("select eid from user where eid='"+eid+"'");          
      System.out.println(rs.next()+ " rs.next");

      if(rs.next() != true)
      {

            System.out.println("rs.next");
           //until this point it works but gives an exception
           if(rs.getString(3).equals("Admin"))
           {

                System.out.println("if admin");

                home.setEnabled(true);
                job.setEnabled(true);
                employee.setEnabled(true);
                user.setEnabled(true);
                machine.setEnabled(true);
                branch.setEnabled(true);

            }
            else if(rs.getString(3).equals("user"))
            {
                System.out.println("else if");

                home.setEnabled(true);
                job.setEnabled(false);
                employee.setEnabled(false);
                user.setEnabled(false);
                machine.setEnabled(true);
                branch.setEnabled(false);
                System.out.println("user can't see");
            }
            else
            {
                 System.out.println("else");
            }
       }
       else
       {
           System.out.println("outer else executed");
                  }
    } 
    catch (Exception ex) 
    {
        Logger.getLogger(Home_Page.class.getName()).log(Level.SEVERE, null, ex);
    }

}

通过这个我试图过滤能够为普通用户点击我的Home_Page中出现的jbuttons,同时允许管理员查看并点击它们。 但是在第一次之后如果它不能正常工作。

异常这样说..

 true rs.next
    rs.next
    Model.JDBC@15bc725
    working
    Mar 15, 2017 3:20:39 PM Viewer.Home_Page <init>
    SEVERE: null
    java.sql.SQLException: After end of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
    at Viewer.Home_Page.<init>(Home_Page.java:65)
    at Viewer.login.loginActionPerformed(login.java:119)
    at Viewer.login.access$000(login.java:18)
    at Viewer.login$1.actionPerformed(login.java:70)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

MySQL表名是user,第三列的列名是ulevel,到目前为止在varchar类型下输入的两个值是“user”和“Admin”。能不能请你帮我解决这个问题,让ifs工作,让那些jbuttons无法点击。

3 个答案:

答案 0 :(得分:2)

你打两次rs.next()。 如果您在结果集中只有一条记录,则会导致错误。第二个问题是您访问第三列rs.getString(3)而您只选择一条。

答案 1 :(得分:2)

您的查询返回eid所以只返回一个值,但是rs.getString(3):

rs = j.getData("select eid from user where eid='"+eid+"'");    
//----------------------^^---------------------------------      
if(rs.getString(3).equals("Admin"))
//--------------^-----------------

相反,你必须使用:

if(rs.getString(1).equals("Admin"))
//--------------^-----------------

Isuggest使用PreparedStatement而不是这会导致语法错误或SQL Injection

而不是:

rs = j.getData("select eid from user where eid='"+eid+"'");

你必须使用

rs = j.executeQuery("select eid from user where eid = ?");
rs.setString(1, eid);

注意

就像@Jens在他的回答中所说的那样,如果你使用两个rs.next()但是不能避免hasnext这会在ResultSet中不存在,你将会收到错误,所以为了避免这种情况,你必须重新 - 检查后执行result,如下所示:

System.out.println("----------->" + (result.next()));
result = preparedStatement.executeQuery();

答案 2 :(得分:1)

您只需通过rs.next()方法迭代ResultSet,然后通过任何ResultSet getter获取列值。 例如,

Statement s = conn.createStatement ();
s.executeQuery ("SELECT id, name FROM user");
ResultSet rs = s.getResultSet();
rs = statement.executeQuery();
while (rs.next()) {
    int id = resultSet.getInt("id");
    int name = resultSet.getInt("name");
    // ...
}