Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
4.25/5 (4 votes)
See more:
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.

Java
 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
C#
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
Updated 8-Jun-18 3:33am

Have you registered the driver?
C#
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:***********
Java
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;

2) Load and Register the JDBC driver:***********
Java
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

or you can use
Java
Class.forName("oracle.jdbc.driver.OracleDriver");

3) Connect to database:***********
a) If you are using oracle oci driver,you have to use:
Java
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:
Java
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:
Java
Statement st = conn.createStatement();

b)write query and execute the query:
Java
ResultSet rs = st.executeQuery("SELECT * from student");

5) Close the statement,resultset and connection:**********
Java
rs.close();
st.close();
conn.close();
 
Share this answer
 
v2
Comments
FoxRoot 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
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.

Java
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
Java
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[^]
 
Share this answer
 
v2
Comments
Aditya Mangipudi 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?
FoxRoot 2-Aug-12 8:45am    
I did not understand sorry Aditya. Plesa help me.
Aditya Mangipudi 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)



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