Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Oracle
Hello everyone,
 
I am developing Java project with netbeans. I completed all the parts(GUI and java code) I want to connect to database keep some information in database for example I want to add new user via textbox to database. However I couldnt Connect it.
Here is my code guys please help me that project is so important for me.
 
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try {
    Connection con=null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:1521:orcl",
        "plsql_staj",
        "plsql_staj1");
    Statement s=con.createStatement();
    s.execute("INSERT INTO E_USER VALUES(((u_name,u_pass,u_role)('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')");
    s.close();
    con.close();
   } 
 catch(ClassNotFoundException | SQLException e){}
 
    }                                        
 
There is no error on netbeans. However button does not work. I am adding
import Oracle.jdbc.pool.OracleDataSource;
 
 
this import but this time I am taking "package oracle.jdbc.pool does not exist" error.
 
Please please please. That is my graduation project. If you cannot solve please announce it.
 
Thanks.
Posted 1-Aug-12 3:55am
FoxRoot861
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have you registered the driver?
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Try Following these steps (Check if you missed anything):
1) Import the following packages in your java file:***********
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
2) Load and Register the JDBC driver:***********
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
or you can use
Class.forName("oracle.jdbc.driver.OracleDriver");
3) Connect to database:***********
a) If you are using oracle oci driver,you have to use:
Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle.world", "root", "root");
where oracle.world is the TNSNAMES entry and root is the username and password.
b) If you are using oracle thin driver,you have to use:
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:mydatabase","root", "root");
where localhost is the host,3306 is the port, mydatabase is the database and root is the username and password.
4) Querying the database:**********
a)create statement:
Statement st = conn.createStatement();
b)write query and execute the query:
ResultSet rs = st.executeQuery("SELECT * from student");
5) Close the statement,resultset and connection:**********
rs.close();
st.close();
conn.close();
  Permalink  
v2
Comments
TuranEmre at 2-Aug-12 6:56am
   
Aditaya thanks for your helps. But I am taking another error which is:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.library.path
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Aditaya I don't know how to thank you. That was the best. Thank you thank you so much.
I have some problems again. How could I understan my driver is oci or thin ? I tried both but I am taking "java.sql.SQLException: Io exception: The Network Adapter could not establish the connection" error.
 
   try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:O10G","plsql_staj", "plsql_staj1");
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * from E_USER");
            rs.close();
            st.close();
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(AddNewUser.class.getName()).log(Level.SEVERE, null, ex);
        }
 
When I used the other option which is
Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle.world", "plsql_staj", "plsql_staj1");
I am taking another error which is:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.library.path
Here is my PLSQL connection screen.
http://www.imagetoo.com/images/untitlzhz.jpg[^]
  Permalink  
v2
Comments
Aditya Mangipudi at 2-Aug-12 8:31am
   
Are you running an Oracle database instance on my computer?
 
Can you connect to the database outside of JDBC via a general client such as Toad?
TuranEmre at 2-Aug-12 8:45am
   
I did not understand sorry Aditya. Plesa help me.
Aditya Mangipudi at 3-Aug-12 9:23am
   
Since you are using a localhost connection to the database (jdbc:oracle:thin:@localhost:3306:O10G"), do you the database on your machine? If yes, try connecting to it using a Oracle Database instance like toad. Google on how to connect to oracle database using toad.
If you are not able to connect to database using the tool, you either did not create any database on your machine or something is blocking your connection.

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 458
1 OriginalGriff 344
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 2 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