6 Replies - 186 Views - Last Post: 04 July 2012 - 06:26 PM Rate Topic: -----

#1 maab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-April 12

Difficulty Selecting From Database

Posted 03 July 2012 - 02:02 PM

here i insert in database without any problem but i want when select from database (select user_id from e_user where email= the same email i inserted it )

void insert()
{
   try
        {
            String sql="Insert into e_user (user_id, user_name, membership_no, email,                      
           user_degree, password) values(user_userid_seq.NEXTVAL,?,?,?,?,?)";
            Connection connection = 
         DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "imes", "imes");
            PreparedStatement pst = connection.prepareStatement(sql);  
            pst.setString(1, txtname.getText());
            pst.setString(2, txtmembership.getText());
            pst.setString(3, txtemail.getText());
            pst.setString(4, cbdegree.getSelectedItem().toString());
            pst.setString(5, txtpass.getText());
            pst.execute();
            
            JOptionPane.showMessageDialog(null, "saved");
            
             new Cases().setVisible(true);
              Statement select =  connection.createStatement();
          ResultSet result = select.executeQuery("select user_id from e_user where email= ? ");
            while(result.next())
            {
                int id = result.getInt("user_id");
                JOptionPane.showMessageDialog(null, "Your id is" + id);
            }
            select.close();
            result.close();
            connection.close();
        }
        catch(SQLException | HeadlessException e)
                {
                    JOptionPane.showMessageDialog(null, e);
                }
}



This post has been edited by macosxnerd101: 03 July 2012 - 03:25 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Difficulty Selecting From Database

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: Difficulty Selecting From Database

Posted 03 July 2012 - 02:16 PM

what is user_userid_seq.NEXTVAL supposed to be ?

what is email = ?
("select user_id from e_user where email= ? ");
Was This Post Helpful? 0
  • +
  • -

#3 maab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-April 12

Re: Difficulty Selecting From Database

Posted 03 July 2012 - 02:37 PM

user_userid_seq.NEXTVAL is sequence column of the table
email =? is wrong but i want select user_id where email = what i inserted but i don't know how can i do
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: Difficulty Selecting From Database

Posted 04 July 2012 - 01:37 PM

is it is done just after the insert it will be what is in txtemail.getText()
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2116
  • View blog
  • Posts: 8,813
  • Joined: 20-September 08

Re: Difficulty Selecting From Database

Posted 04 July 2012 - 03:59 PM

Try closing 'pst' immediately after execute

This post has been edited by g00se: 04 July 2012 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: Difficulty Selecting From Database

Posted 04 July 2012 - 04:40 PM

Hi,

To help the forum do you mind re-formatting your text better next time? It's extremely hard to interpret what you're saying. Besides that, I believe this is your problem:

PreparedStatement pst = connection.prepareStatement(sql); 
            pst.setString(1, txtname.getText());
            pst.setString(2, txtmembership.getText());
            [b]String email[/b] = pst.setString(3, txtemail.getText()); //Just set email for one considering that was you problem.
            pst.setString(4, cbdegree.getSelectedItem().toString());
            pst.setString(5, txtpass.getText());
            pst.execute();
             
            JOptionPane.showMessageDialog(null, "saved");
             
           new Cases().setVisible(true);
           Statement select =  connection.createStatement();
           ResultSet result = select.executeQuery("select user_id from e_user where email= " [b]+ email[/b]);


Hope this could help your problem

Also is there a way to edit my posts??
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,543
  • Joined: 27-December 08

Re: Difficulty Selecting From Database

Posted 04 July 2012 - 06:26 PM

Quote

Also is there a way to edit my posts??

Not until you hit a certain number of posts. More on our editing policies:

http://www.dreaminco...-to-edit-times/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1