Results 1 to 3 of 3
Thread: first time JDBC connection
- 04-28-2013, 03:28 PM #1
Member
- Join Date
- Dec 2010
- Location
- Ireland
- Posts
- 12
- Rep Power
- 0
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 advanceLast edited by Darego; 04-28-2013 at 09:46 PM.
- 04-28-2013, 09:44 PM #2
Member
- Join Date
- Dec 2010
- Location
- Ireland
- Posts
- 12
- Rep Power
- 0
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
- 04-29-2013, 12:18 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Similar Threads
-
JDBC connection
By Dineshmtech in forum JDBCReplies: 2Last Post: 01-07-2012, 09:20 AM -
JDBC Connection
By Riyaz.hk77 in forum New To JavaReplies: 2Last Post: 11-06-2010, 06:53 PM -
JDBC connection
By evermore in forum JDBCReplies: 1Last Post: 03-19-2010, 09:35 AM -
JDBC Connection...
By onlysumitg in forum JDBCReplies: 3Last Post: 08-21-2009, 12:34 PM -
JDBC connection
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:39 PM
Bookmarks