Results 1 to 3 of 3
  1. #1
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default first time JDBC connection

    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 project 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

    Java Code:
    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");
    				ods.setUser("SYSTEM");
    				ods.setPassword("db27Oct90");
     
    				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

    Java Code:
    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
    Last edited by Darego; 04-28-2013 at 08:46 PM.

  2. #2
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default Re: first time JDBC connection

    edited previous post to clear some things up.

    this is what i get in the console when i run CreateContacts class:

    Java Code:
    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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: first time JDBC connection

    If you get an exception please post the full exception including the stacktrace.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JDBC connection
    By Dineshmtech in forum JDBC
    Replies: 2
    Last Post: 01-07-2012, 08:20 AM
  2. JDBC Connection
    By Riyaz.hk77 in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 05:53 PM
  3. JDBC connection
    By evermore in forum JDBC
    Replies: 1
    Last Post: 03-19-2010, 08:35 AM
  4. JDBC Connection...
    By onlysumitg in forum JDBC
    Replies: 3
    Last Post: 08-21-2009, 11:34 AM
  5. JDBC connection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •