Results 1 to 20 of 21
Thread: JDBC and SQL
- 10-30-2010, 04:18 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
JDBC and SQL
Hi,
Can anyone help me, I'm trying to do a program using the JDBC..I'm trying to create tables using SQL but I'm not sure what to write in the path, username, and password field????
Here is what I wrote...
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("driver", "username", "password");
result = true;
}
catch(ClassNotFoundException e) {
System.out.println("Unable to load the driver class");
result = false;
}
when I try to start the program, it goes to the catchException message...
Can anyone advice please, what should I do???
- 10-30-2010, 06:37 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
See this page...conn = DriverManager.getConnection("driver", "username", "password");
http://www.rgagnon.com/javadetails/java-0112.html
- 10-30-2010, 09:15 AM #3
Member
- Join Date
- Oct 2010
- Location
- Nagpur
- Posts
- 6
- Rep Power
- 0
I don't know about the jdbc and faced always problem at the time connectivity because I am trying self and many problem arises so i want your help to do best connectivity.
- 11-02-2010, 10:28 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
Try this
try{
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DSN");
Statement st=con.createStatement("Select * from emp");
}
- 11-02-2010, 02:19 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
- 11-02-2010, 03:13 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
No, don't do this.
You have a prefectly good JDBC driver designed for Oracle. Using the JDBC/ODBC bridge is a major step backwards.
If you're going to answer questions at least make sure you know what you're talking about.
As for the original problem, first off the you need to print the full exception (e.printStackTrace()). That'll tell you exactly what's wrong.
I'm guessing that you haven't got the oracle driver jar in your runtime classpath...in which case you need to tell us how you are running this.
On top of that you need to read up on Oracles driver to check what format the url for getConnection needs to be in. It's different for each driver.
- 11-04-2010, 05:43 AM #7
Member
- Join Date
- Oct 2010
- Location
- Nagpur
- Posts
- 6
- Rep Power
- 0
j2ee
I want to try for job in my hometown and my interest in development using java language so I want some questions which is minor and generally not in candidate mind when he/she prepare for interview. So I want your help sir.
- 11-04-2010, 08:31 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You ought to open your own thread on that then, not hijack someone elses.
- 11-07-2010, 06:58 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Thank you so much, I've moved from this problem and I created the connection and I created my tables as well... but I have now another problem in the execution time....I got this error:
java.sql.SQLException: ORA-00955: name is already used by an existing object
What that mean???? therefore, the tables are not initiated?
- 11-07-2010, 09:37 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Quoting the docs:
You are reusing a table name or something similar.An attempt was made to create a database object (such as a table, view, cluster, index, or synonym) that already exists. A user's database objects must have distinct names.
- 11-07-2010, 07:28 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Thanks for reply, I changed my tables name in the code to solve this problem, but now I have another error, and I don't know how to solve it. I'm working on it remotely. Is this could be a problem???
This is how I get the connection...
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@bajor.engr.scu.edu:1521:db9 i",
"username", "password");
Error:
Io Exception: the network adapter could not establish the connection
Please advice???
- 11-08-2010, 11:35 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Something's wrong with your URL then.
That's what that error means.
Usually port number or your db sid, since I think you get a different error if the server ip is wrong.
According to that yous sid is "db9 i". Is that space really there in your code or is it a forum glitch?
- 11-08-2010, 05:50 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
- 11-09-2010, 06:50 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Retrieve data from Tables...
Hi again,
Now, I'm facing a slight problem. I wish that anyone can help me please... I created tables, and now I'm trying to fill inside it within the code. But for a reason it is not going into the method initTables at all... it only display 2 names out of 15????
This is an initiation of two tables....
try {
Statement stmt = conn.createStatement();
for (int i = 0; i < SQLData.length; i++) {
iRowCount = iRowCount+1;
iRowCount= stmt.executeUpdate("INSERT INTO authors VALUES " + SQLData[i] );
}
System.out.println( iRowCount +" Rows are inserted.");
PreparedStatement pstmtPublishers = conn.prepareStatement("INSERT INTO publishers(publisherID,publisherName) VALUES (?,?)");
for(int i=1; i <= 15; i++) {
pstmtPublishers.setInt(1, i);
pstmtPublishers.setString(2, "Publisher" + Integer.toString(i));
pstmtPublishers.executeUpdate();
}
pstmtPublishers.close();
I appreciate any help ....
- 11-09-2010, 07:39 AM #15
put some debugging statements(s.o.p)
Paste ur complete code.
For each and evry method,write a comment on top of that so that it will be easy for you as well fopr the reviewer to give comments.Ramya:cool:
- 11-09-2010, 08:12 AM #16
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Here are the link for the code that I made, actually I'm not getting any error but I think I have a problem in initTable method, because It's not getting all the data.
BooksDB.java - File Shared from Box.net - Free Online File Storage
BooksDBConsole.java - File Shared from Box.net - Free Online File Storage
If you can help me please, I don't know what is the problem.
- 11-09-2010, 08:49 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
For this code:
How many inserts are called (you'll need to put in the debug stuff Ranya suggested to see.Java Code:for (int i = 0; i < SQLData.length; i++) { iRowCount = iRowCount+1; iRowCount= stmt.executeUpdate("INSERT INTO authors VALUES " + SQLData[i] ); }
Also what are the actual INSERT statements...print them out as you go.
I suspect one of them is failing because you are not using a PreparedStatement here and are concatenating names into your INSERT statement, and one of them has something odd about it.
- 11-09-2010, 05:48 PM #18
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
The output
This is what I get when I run the program...
------------------------------------------------------------------------------------
Tables created successfully
Unable to init tables
java.sql.SQLException: ORA-00911: invalid character
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBE rror.java:114)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:2 08)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:542)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol .java:1311)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TT C7Protocol.java:738)
at oracle.jdbc.driver.OracleStatement.executeNonQuery (OracleStatement.java:1313)
at oracle.jdbc.driver.OracleStatement.doExecuteOther( OracleStatement.java:1232)
at oracle.jdbc.driver.OracleStatement.doExecuteWithBa tch(OracleStatement.java:1353)
at oracle.jdbc.driver.OracleStatement.doExecute(Oracl eStatement.java:1760)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTi meout(OracleStatement.java:1805)
at oracle.jdbc.driver.OracleStatement.executeUpdate(O racleStatement.java:531)
at BooksDB.initTables(BooksDB.java:84)
at BooksDBConsole.main(BooksDBConsole.java:18)
WELCOME TO BOOKS DATABASE.
Please Select from the options below:
1 - List all authors.
2 - List all publishers.
Please enter a menu option now:
1
Following are the authors names sorted alphabetically by last name and first name:
1, Khodary , Abdulaziz
2, Khodary , Abdulaziz
Please enter a menu option now:
---------------------------------------------------------------------------
The problem I think in filling the tables, because it goes inside thr for loop and stopped in the second row for the first table (I defined 15 names). and it is not going to the second loop for the second table.
I'm sure there is somethig wrong, but I tried several options and still not working...Please advice.
- 11-09-2010, 08:20 PM #19
Member
- Join Date
- Oct 2010
- Posts
- 26
- Rep Power
- 0
Almost...
Thank you so much for your advice...Now, it started to fill the entire table. Now, my question is how I can retrieve from the array...
For example:
String[] Data={"(1,'F1','L1')","(2,'F2','L2')"};
If I want this array to be filled in this code...Can you advice me how I can do that???
PreparedStatement stmt = conn.prepareStatement("INSERT INTO authors (ID,firstname,lastname) VALUES (?,?,?)");
for (int i=1; i<=Data.length;i++) {
stmt.setInt(1,i);
stmt.setString(2, how can I retrieve the first name from the array),
stmt.setString(2, how can I retrieve the last name from the array),
stmt.exceuteUpdate()
}//End for
stmt.close();
-----------------------------------
I really appreciate the help and support you give...
Thank you
- 11-10-2010, 04:09 AM #20
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I think what you need is a 2D array.
Java Code:String[] Data={"(1,'F1','L1')","(2,'F2','L2')"};Java Code://String[here to fill what [b]row[/b]][here to fill what [b]column[/b] of the specified row] String[][] Data={{1,"F1","L1"}, //<---- Row 1 {2,"F2","L2"}}; //<---- Row 2
Also, you are using String[] but its value has an integer.
Java Code:[b]String[/b][][] Data={{[b]1[/b],"F1","L1"}, {[b]2[/b],"F2","L2"}};Last edited by mine0926; 11-10-2010 at 04:11 AM.
Similar Threads
-
Jdbc
By KumbhaniMehul in forum Java ServletReplies: 1Last Post: 04-06-2010, 06:18 PM -
JDBC for DB2
By mostafa-sky in forum JDBCReplies: 1Last Post: 03-21-2009, 05:17 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
Jdbc
By siwa in forum JDBCReplies: 4Last Post: 11-09-2007, 07:31 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks