Jump to content

  • Log In with Google      Sign In   
  • Create Account


first time JDBC connection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Darego   Members   -  Reputation: 156

Like
0Likes
Like

Posted 28 April 2013 - 06:37 AM

Hi guys,

just installed oracle database express edition for my home computer. i have the database running and i am connected though SQL developer. i couldn't connect via the basic connection as i kept getting an error (listener refused connection... TNS: listener does not know of SID given connect descriptor) so i connected through a TNS connection with network alias: ORCL 

my problem is now i don't know how to connect my java code to the database so i can run CreateContacts class to make the tables ... currently when i run the CreateContacts class and check in SQL developer if the tables were made, they are not in the database so i assume my java connection(// Home Oracle XE) is incorrect in the DBConnection class. can anyone have a look please and help me get my java connected to my database so i can run the CreateContacts class?

DBConnection class

 

package database;
  
import java.sql.*;
  
import oracle.jdbc.pool.OracleDataSource;
  
public class DBConnection {
  
    private Connection conn = null;
  
    public Connection openDB()
    {
        try
            {   
                OracleDataSource ods = new OracleDataSource();
  
  
                // College
                // ods.setURL("jdbc:oracle:thin:@//10.10.2.7:1521/global1");
                // ods.setUser("x00075734");
                // ods.setPassword("db27Oct90");
  
                // Home Oracle XE
                ods.setURL("jdbc:oracle:thin:HR/SYSTEM@localhost:1521:XE"); // not sure if this line is
                                                                            //is correct
                ods.setUser("SYSTEM");  // username i use to log into oracle database
                ods.setPassword("db27Oct90"); //password i use to log into oracle database
  
                conn = ods.getConnection();
                System.out.println("connected.");               
            }
        catch (Exception e)
            {
                System.out.print("Unable to load driver " + e);
                System.exit(1);
            }   
        return conn;
    }
    public void closeDB()
    {
        try
            {   
                conn.close();
                System.out.print("Connection closed");
            }
        catch (SQLException e)
            {
                System.out.print("Could not close connection ");
                e.printStackTrace();
            }
    }
}

 

 

CreateContacts class

 

package database;
  
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
  
public class CreateContacts
{ 
    private Connection conn = null;
    private PreparedStatement stmt = null;
    private DBConnection db;
  
    public CreateContacts()
    {
        db = new DBConnection();
        conn = db.openDB();
    }   
    public void CreateContactsTable()
    {
        try
            {           
                // Dropping a Table
                String drop = "DROP TABLE Contacts";
                stmt = conn.prepareStatement(drop);
                stmt.executeUpdate();
  
                // Create a Table
                String create = "CREATE TABLE Contacts " +
                        "(id NUMBER PRIMARY KEY, name VARCHAR(40), address VARCHAR(30), pnumber VARCHAR(30), email VARCHAR(20))";
                stmt = conn.prepareStatement(create);
                stmt.executeUpdate(create);
  
                // Insert data into table
  
                String insertString = "INSERT INTO Contacts(id,name,address,pnumber,email) values(?,?,?,?,?)";          
                stmt = conn.prepareStatement(insertString);
  
                stmt.setInt(1, 1);
                stmt.setString(2, "Peter");
                stmt.setString(3, "23 Lime Lane");
                stmt.setString(4, "018776543");
                stmt.setString(5, "p.cassisy@b.com");               
                stmt.executeUpdate();
  
                stmt.setInt(1, 2);
                stmt.setString(2, "Donal");
                stmt.setString(3, "2 Shelbourne rd");
                stmt.setString(4, "012445678");
                stmt.setString(5, "d.oreilly@b.com");               
                stmt.executeUpdate();
  
                stmt.setInt(1, 3);
                stmt.setString(2, "Mary");
                stmt.setString(3, "4 Richmond rd");
                stmt.setString(4, "018765456");
                stmt.setString(5, "m.lawlor@b.com");                
                stmt.executeUpdate();
  
                stmt.setInt(1, 4);
                stmt.setString(2, "Glen");
                stmt.setString(3, "4 Richmond lane");
                stmt.setString(4, "017854563");
                stmt.setString(5, "g.whelan@b.com");                
                stmt.executeUpdate();
  
                conn.commit();
                stmt.close();
                db.closeDB();
            }       
            catch (SQLException e)
            {
                System.out.print("SQL Exception " + e);
                System.exit(1);
            }   
    }   
    public static void main(String args[]) 
    {
        CreateContacts ct = new CreateContacts();
        ct.CreateContactsTable();   
    }
  
}

 


thanks in advance


Edited by Darego, 28 April 2013 - 01:56 PM.


Sponsor:

#2 Darego   Members   -  Reputation: 156

Like
0Likes
Like

Posted 28 April 2013 - 12:49 PM

edited previous post to clear some things up.

this is what i get in the console when i run CreateContacts class. does this mean that the code is not connected to the database?

 

testing US7ASCII against <abc>
    PASSED LOSSY
testing US7ASCII against <ab?c>
    PASSED LOSSY
testing US7ASCII against <XYZ>
    PASSED LOSSY
testing US7ASCII against <longlonglonglong...>
    PASSED LOSSY
testing WE8ISO8859P1 against <abc>
    PASSED LOSSY
testing WE8ISO8859P1 against <ab?c>
    PASSED LOSSY
testing WE8ISO8859P1 against <XYZ>
    PASSED LOSSY
testing WE8ISO8859P1 against <longlonglonglong...>
    PASSED LOSSY
testing AL24UTFFSS against <abc>
    PASSED 
testing AL24UTFFSS against <ab?c>
    PASSED 
testing AL24UTFFSS against <XYZ>
    PASSED 
testing AL24UTFFSS against <longlonglonglong...>
    PASSED 
testing UTF8 against <abc>
    PASSED 
testing UTF8 against <ab?c>
    PASSED 
testing UTF8 against <XYZ>
    PASSED 
testing UTF8 against <longlonglonglong...>
    PASSED

Edited by Darego, 28 April 2013 - 01:54 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS