0

I'm currently working on this dynamic web project. I have a database and table that I've created in Oracle.

What I need to do right now is have this table connected to my project so that I can retrieve the data from there.

I read that I will need a JDBC driver downloaded and I found it here

But, it's not clear which one is the right one to download and where should I place it after that? in connection pool through admin console?

all toturials I see are related to mySql even this one:

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

      // JDBC driver name and database URL
      static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
      static final String DB_URL="jdbc:mysql://localhost/TEST";

      //  Database credentials
      static final String USER = "root";
      static final String PASS = "password";

How can I use the same thing for oracle?

Yura
  • 125
  • 10
  • The Oracle JDBC driver needs to be on your class path, wherever that is with your current setup. As for which driver to download, just pick the one recommended for your version of Oracle. – Tim Biegeleisen Apr 12 '18 at 06:12
  • I'm using latest ver. of oracle sql developer, where to place it in the class bath? you mean with the jar files in my project in eclipse? – Yura Apr 12 '18 at 06:14
  • Yes, that sounds like it might work. I don't use Eclipse though, I use IntelliJ. – Tim Biegeleisen Apr 12 '18 at 06:16
  • so which version do you recomment, do you think ojdbc8.jar is ok? by the way i'm using jdk 1.6 – Yura Apr 12 '18 at 06:17
  • I have no idea...check the Oracle website to be certain. – Tim Biegeleisen Apr 12 '18 at 06:18
  • JDK 1.6? In 2018? Why on earth are you doing that? The current Java version is 10. Java 6,7 and 8 are deprecated. You can't even download anything before Java 8. You should **really** switch to a current Java version. At **least** Java 8. `ojdbc8.jar` is intended for Java **8** so you can not use that with your ancient Java version. You would need `ojdbc6.jar` - but again: don't stick with Java 6! –  Apr 12 '18 at 06:55

2 Answers2

1

It is a example, how to connect to DB and to retrieve data from DB. I hope it is useful for you. Don't forget about try catch finally. Read this topic. Closing Database Connections in Java

Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe"; //127.0.0.1 = localhost, 1521 = standard port, xe - DB name
String user = "root"; 
String password = "password";
Connection con = DriverManager.getConnection(url, user, password);

//To create sql query
PreparedStatement preparedStatement = con.prepareStatement("SELECT * FROM person");

//Response of your sql query
ResultSet resultSet = preparedStatement.executeQuery();
//For example you have table (Int id, String firstName, String  lastName )
while(resultSet.next()){
    //Prepare your data with your program logic....
    int id = resultSet.getInt(1);
    String firstName = resultSet.getString(2);
    String lastName = resultSet.getString(3);
    Person p = new Person(id, firstName, lastName);
}
  • doesn't it need to be uploaded on the server (mine is glassfish) so that it can have a url?? – Yura Apr 12 '18 at 07:33
  • _I'm using latest ver. of oracle sql developer, where to place it in the class bath?_ if you use oracle sql developer you can see your connect details on DataBase Connection Properties. [see image](http://prntscr.com/j45tto) my url = "jdbc:oracle:thin:@192.168.238.205:1521:aquariustest" – Vasile Razdalovschi Apr 12 '18 at 07:41
  • it gives error (java.lang.UnsupportedClassVersionError: oracle/jdbc/driver/OracleDriver : Unsupported major.minor version 51.0) do you think my ojdbc6 file is not placed in the right path ?? – Yura Apr 12 '18 at 07:50
  • by the way, i did not create a connection pool i just put the url, username and password using the code you just posted – Yura Apr 12 '18 at 07:53
  • _I use IntelliJ._ File -> Project Structure(CTRL+ALT+Shift+S) -> Libraries -> +(New project Library) -> Java -> (and Select your OJDBC) after verify imports in project – Vasile Razdalovschi Apr 12 '18 at 07:54
  • I use eclipse, generally i need to locate it with the jar files in my project right? – Yura Apr 12 '18 at 07:57
  • Come on without blind copying. I don't know that you need to retrieve from DB and that to do with these records. I gave you a example how to connect to DB and to execute a Query. – Vasile Razdalovschi Apr 12 '18 at 07:58
  • lol i'm not blind copying, of course i edited it based on the current database and table i'm working on – Yura Apr 12 '18 at 08:00
  • looks like we both are working on similar eniroments i see that most people are working on mySql management studio. so please can you mention the steps you do after creating the table, retrieve the data through your code? i use oracle Sql developer and my server is glassfish – Yura Apr 12 '18 at 08:16
0

Can you check the JDK version and the JDBC driver version that you are using? Both should be compatible. Check out the FAQ for more information.

Nirmala
  • 1,278
  • 1
  • 10
  • 11