Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


#ActualDarego

Posted 28 April 2013 - 01:56 PM

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


#3Darego

Posted 28 April 2013 - 12:49 PM

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

 

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


#2Darego

Posted 28 April 2013 - 06:40 AM

Hi guys,

just downloaded oracle database express edition for my home computer. i installed it and have the database control manager open. our lecturer in college asked us to connect to the database and create a table but i am not sure if i am connected correctly to the database.. can someone let me know if i am doing something wrong?

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();   
    }
  
}

 

 

when i run the CreateContacts class all i get is:

"testing US7ASCII against <abc>
PASSED LOSSY" etc etc and i'v no idea what that is suppose to mean. does it mean i'm not connected to the database correctly?

so my questions: am i connected to the oracle express database? did running the class create the tables? i'm not sure how to check with the database manager.. what do i open from oracle express so i can give sql statements to see if i have created any tables

thanks in advance


#1Darego

Posted 28 April 2013 - 06:37 AM

Hi guys,

just downloaded oracle database express edition for my home computer. i installed it and have the database control manager open. our lecturer in college asked us to connect to the database and create a table but i am not sure if i am connected correctly to the database.. can someone let me know if i am doing something wrong?

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();   
    }
  
}

 

 

when i run the CreateContacts class all i get is:

"testing US7ASCII against <abc>
PASSED LOSSY" etc etc and i'v no idea what that is suppose to mean. does it mean i'm not connected to the database correctly?

so my questions: am i connected to the oracle express database? did running the class create the tables? i'm not sure how to check with the database manager

thanks in advance


PARTNERS