Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone. I am developing a desktop application via netbeans. I need to create a database. I hava tried oracle. However I couldn't become successful. so I searched and decided on JavaDB which is within Netbeans. I have used a guide to create the database. Now I want to insert data via interface. I have a form which includes 2 textboxes (username and user password) and a combobox (user role). I have done something but does not work. Could you help me ?

Java
try
         { 
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
         }catch(ClassNotFoundException e)
         {
            System.out.println(e);
         }
        
        try {
            //1.Kısım Baglantı Saglıyoruz
            this.host = "jdbc:derby://localhost:1527/EMRE";
            this.Name = "emre";
            this.Pass = "2561";
            Connection con = DriverManager.getConnection(host,Name,Pass);
             
            //2.Kısım Statement Olusturuyorum
            
            Statement cumle = con.createStatement();
            String query = "INSERT INTO P_LOGIN VALUES('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')";
            ResultSet rs = cumle.executeQuery(query);
          
            
            
            }
        catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }


This is my ADD button code. Where am I doing wrong? Please show me a way.
Posted
Comments
Raje_ 7-Aug-12 2:46am    
what error are you getting?

Here you are using createStatement() method, because you are inserting values to database you need to use prepareStatement method:-
Java
PreparedStatement ps = scon.prepareStatement("INSERT INTO P_LOGIN VALUES(?,?,?)");
          ps.setString(1, txtAddUserName.getText());
          ps.setString(2, txtAddUserPassWord.getText());
          ps.setString(3, cmbAddUserRole.getSelectedItem().toString());
          int i = ps.executeUpdate();
          if (i > 0)
          {
             //do your stuff
          }


For dealing with JavaDB, Please go through this link:-
Working with the Java DB (Derby) Database[^]

Good luck.
 
Share this answer
 
v2
Comments
FoxRoot 7-Aug-12 3:06am    
I have already read this article. However, ı am working with interface. This does not help. Thanks.
TorstenH. 7-Aug-12 3:16am    
you are "working with interface" ??? hmm, interesting.
FoxRoot 7-Aug-12 3:25am    
TorstenH you helped me too much. But you don't understand me. I am asking question after I made research.
So please be more serious. English is not my mother tonque.
TorstenH. 7-Aug-12 3:37am    
Then try to explain what you mean by "working with interface".
That's as explicit as "working with computers".
FoxRoot 7-Aug-12 3:40am    
I am sorry. I mean that I am working with jFrame. Namely I want to get data via textboxes and save them into database. That is all that I want to.
I'm with Rajesh - Derby rules! +5 for that.

But I guess your google is broken and you are searching for this tutorial:
Working with the Java DB (Derby) Database[^]
 
Share this answer
 
Java
try {
            //1.Kısım Baglantı Saglıyoruz
            this.host = "jdbc:derby://localhost:1527/EMRE";
            this.Name = "emre";
            this.Pass = "2561";
            Connection con = DriverManager.getConnection(host,Name,Pass);
             
            //2.Kısım Statement Olusturuyorum
            
            Statement cumle = con.createStatement();
            
            cumle.executeUpdate("INSERT INTO P_LOGIN " + " (SIFRE, KULLANICI_ADI, ROLE)" + " VALUES ('"+txtAddUserPassWord.getText()+"','"+txtAddUserName.getText()+"','"+cmbAddUserRole.getSelectedItem()+"')");             
            
            }
        catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }


Thanks to TorstenH. and Rajesh.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900