Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By SurfMan

Thread: Makes me cast result

  1. #1
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Makes me cast result

    i am new in netbeans, suffering same problems, i am trying to cod a simple query on db, it makes me to cast result which is not necessary, it dosen't know result.next() plz inform me when you found your answer.

    Java Code:
     public boolean query_on_DB( String _query )
        {
            try 
            {
            stmt= conn.createStatement();
            resSet = stmt.executeQuery( _query );
            if(resSet.next())
            {
                return true;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "nothing! ");
                return false;
            }
                
            } catch (Exception e) 
            {
                JOptionPane.showMessageDialog(null, "can not execut query! ");
                return false;
            }
            }
        
        
        /**
         * Creates new form main
         */
        public main() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jPanel2 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            txtUserName = new javax.swing.JTextField();
            txtPassword = new javax.swing.JPasswordField();
            jLabel2 = new javax.swing.JLabel();
            jLabel1 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18), new java.awt.Color(51, 51, 255))); // NOI18N
    
            jButton1.setText("Login");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("password ->");
    
            jLabel1.setText("username ->");
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtPassword))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap())
            );
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(267, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(235, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String uName = txtUserName.getText();
            String uPass = txtPassword.getText();
            
            String sql = "select * from users where user_uname='"+uName+"'"+ "and user_upass='"+uPass+"'";
                    
            
            if(connect_to_DB())
            {
                if(query_on_DB(sql))
                {
                    JOptionPane.showMessageDialog(null, "OK!");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "incorrect info!");
                }
            }
    Attached Files Attached Files
    Last edited by saprz; 03-24-2017 at 02:02 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Makes me cast result

    When posting code, please wrap it in code tags (I've done it for you this time).

    Are you getting errors with this code?
    If so, can you post the full error message and highlight the line it occurs on?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    Thanks for your assist, my problem is netbeans itself, this cod has no error in eclips, but it doesn't work in netbeans, cause it does n't know resSet = stmt.executeQuery( _query ); without casting
    also dos n't know next() in result.next(), eclips does it with just importing java.sql.*, netbeans mad me so desperate, i'll be greatful if you help me whit this.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Makes me cast result

    We're only seeing half of your code. Apparently, conn, stmt and resSet are all class variables (fields). Are they really there and are they of the correct type? (e.g. not all declared as Object).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Makes me cast result

    This code has used a gui builder.
    Do the Eclipse and Netbeans builders work together?

    Anyway, you haven't said the exact error message you are getting.
    What are the imports?

    I expect you have imported something when you moved the projects over.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    dear surfman i just have problem with resSet = stmt.executeQuery( _query ); in netbeans, it works in eclips without any cast, and also next() in result.next() and i think it's sort of library problem.

  7. #7
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package my_java_app;
    
    
    import javax.naming.spi.DirStateFactory;
    import javax.xml.transform.Result;
    import java.sql.*;
    
    import javax.swing.JOptionPane;
    
    import java.util.Properties;
    
    import java.beans.Beans;
    
    
    
    /**
     *
     * @author SAEED
     */
    public class main extends javax.swing.JFrame {
        
        private Connection conn=null;
        private Statement stmt;
        private Result resSet=null;
        
        public boolean connect_to_DB()
        {
            try 
            {
                String userName = "root";
                String password = "";
                String url = "jdbc:mysql://localhost/jdatabase";
                
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = DriverManager.getConnection(url, userName, password);
                return true;
                
            } catch (Exception e) 
            {
                JOptionPane.showMessageDialog(null, "can not connect to DB! ");
                return false;
            }
        }
        
        
        public boolean query_on_DB( String _query )
        {
            try 
            {
            stmt= conn.createStatement();
            resSet = stmt.executeQuery( _query );
            if(resSet.next())
            {
                return true;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "nothing! ");
                return false;
            }
                
            } catch (Exception e) 
            {
                JOptionPane.showMessageDialog(null, "can not execut query! ");
                return false;
            }
            }
        
        
        /**
         * Creates new form main
         */
        public main() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jPanel2 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            txtUserName = new javax.swing.JTextField();
            txtPassword = new javax.swing.JPasswordField();
            jLabel2 = new javax.swing.JLabel();
            jLabel1 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18), new java.awt.Color(51, 51, 255))); // NOI18N
    
            jButton1.setText("Login");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("password ->");
    
            jLabel1.setText("username ->");
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtPassword))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap())
            );
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(267, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(235, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String uName = txtUserName.getText();
            String uPass = txtPassword.getText();
            
            String sql = "select * from users where user_uname='"+uName+"'"+ "and user_upass='"+uPass+"'";
                    
            
            if(connect_to_DB())
            {
                if(query_on_DB(sql))
                {
                    JOptionPane.showMessageDialog(null, "OK!");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "incorrect info!");
                }
            }
                    
        }                                        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new main().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPasswordField txtPassword;
        private javax.swing.JTextField txtUserName;
        // End of variables declaration                   
    
     
    
    
    }
    problem is these 2 lines:

    resSet = stmt.executeQuery( _query );
    if(resSet.next())
    Last edited by saprz; 03-24-2017 at 02:27 PM.

  8. #8
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    i meant same cod whit different GUI,

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,997
    Rep Power
    32

    Default Re: Makes me cast result

    Please use CODE tags not QUOTE tags
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Makes me cast result

    Java Code:
    import javax.xml.transform.Result;
    import java.sql.*;
    Well, since you moved the code from Eclipse you have clearly added that import statement for javax.xml.transform.Result.
    Otherwise your code would have the same issues in Eclipse as it does at the moment in Netbeans.

    So, since I can't see anywhere you might be wanting to use XML transformation, remove that import statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    THANKS, but problem still exist

  12. #12
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    Oops, Sorry, i did that

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,997
    Rep Power
    32

    Default Re: Makes me cast result

    Why is resSet defined as a Result and not a ResultSet?
    saprz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    That's exactly like you saved my life, i was going crazy whit a lot of work above my head whit no eye to see monitor, thank you sooooo much

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Makes me cast result

    So how did it change between IDEs?
    It wouldn't have done that by magic...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    saprz is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Makes me cast result

    just believe in magic...

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Makes me cast result

    Quote Originally Posted by saprz View Post
    just believe in magic...
    Considering the code we see on this forum, some people do indeed need magic to get their code fixed.
    DarrylBurke likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  2. classpath - when compiler makes use of that?
    By Ann22 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 09:58 AM
  3. Never makes it to the method!
    By minihazard10 in forum New To Java
    Replies: 9
    Last Post: 11-10-2008, 07:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •