Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Oracle Text
Hello everyone.
I have a problem I have searched too much but I can't solve this. I am using netbeans and I have an oracle database. This is my first time to connect an application with a database. Simply, I have a form at netbeans and a table at database.
 
http://www.imagetoo.com/images/ss1ksk.png[^] In here I have 2 textboxes and a combobox whic has 2 items (administrator and instructor). I want to code under the add button. Namely, I want to save user name password and its role in a table. Here I have created a table.
 
http://www.imagetoo.com/images/ss2mdm.png[^]
 
I am noob guys, please help me. If you teach me how to insert I will develop delete update etc.
 
//UPDATE// here is my button code
 
try {
      Connection con=null;
      Class.forName("oracle.jdbc.driver.OracleDriver");
      con=DriverManager.getConnection(
        "jdbc:oracle:thin:@AND ALSO HERE:1521:O10G",
        "plsql_staj",
        "plsql_staj1");
      Statement s=con.createStatement();
      s.execute("INSERT INTO E_USER VALUES(""I HAVE PROBLEM HERE")");
      s.close();
      con.close();
   } 
 catch(ClassNotFoundException | SQLException e){}
 

 

Thanks.
Posted 31-Jul-12 20:49pm
FoxRoot861
Edited 31-Jul-12 21:15pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First setup a connection to your database.
Before trying to actually access the database from your program, try to connect to it using sqlplus from your development machine. This way you know that the connection string and tnsnames.ora is correct.
There is tons of information about this on the web.
 
http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html[^]
 
Then when it comes to inserting here is another link :
 
http://www.devdaily.com/java/edu/pj/jdbc/jdbc0002[^]
 
Also here there is a ton of information on the web.
  Permalink  
Comments
TuranEmre at 1-Aug-12 3:13am
   
Thanks Philip as I mentioned I have searched a lot but something is wrong. Here is my button code. (Updated into question)
Philip Stuyck at 1-Aug-12 3:30am
   
Your insert statement should be something like this :
insert into e_user values('bla','bla','bla')
Like the second example I posted you have to assemble the string dynamically. There are other ways using parameters but this is a little more advanced.
I really think that with this last link I provided it should help you get this working.
TuranEmre at 1-Aug-12 3:40am
   
Yes I see but how could I send textboxes? insert into e_user values('bla','bla','bla') this is easy I want to solve this
s.execute("INSERT INTO E_USER VALUES(((u_name,u_pass,u_role)('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')");
Philip Stuyck at 1-Aug-12 4:03am
   
your insert statement looks wrong to me since you are specifying the column names in the values.
Look at this :
http://psoug.org/definition/INSERT.htm
bottom line is
INSERT INTO users
(first_name, last_name)
VALUES
('john', 'smith');
Philip Stuyck at 1-Aug-12 4:08am
   
As a general tip. Try out your insert statement manually first. If you can insert it manually in the database, that sql statement is the one you need to create by code. You can also use a temp string variable and print it or debug it to see what is actually the string you are using for your insert.
TuranEmre at 1-Aug-12 10:32am
   
Philip you don't understand neither me nor my question. This is an Form application. Instead of john I have to use txtAddUserName.getText(). Because It may be john Philip or Turan. What the user wants? I tried it manually it works with static names but I need to use it dynamically via textbox.
Thanks.
Philip Stuyck at 1-Aug-12 11:02am
   
don't you see that your string that you are passing in s.execute is completely wrong ?
You are doing something like INSERT INTO E_USER VALUES(((u_name,u_pass,u_role)('bla','bla','bla'))) This is just plain wrong SQL syntax.
It should be more like
s.execute("INSERT INTO E_USER(u_name,u_pass,u_role) VALUES('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')");
or not mention the column names at all.
s.execute("INSERT INTO E_USER VALUES('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')");
will also work.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 489
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 PIEBALDconsult 150
0 OriginalGriff 7,755
1 Sergey Alexandrovich Kryukov 6,951
2 Maciej Los 3,994
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100