0

I must encapsulate my access to the Oracle database in one java class, called dbconf:

package DB_Oracle_Connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;




  public class dbconf {

  private String connstr;
  private Connection connect;
  private PreparedStatement prepstat = null;

  public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@localhost:1521:orcl";

        try {
                String uname = "fred";
                String pass = "flintstone";


                Class.forName("oracle.jdbc.driver.OracleDriver");


                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }
        if (connect != null) {
            System.out.println("Connected to the database!");
        } else {
            System.out.println("Failed to make connection!");
        }

            return connect;
    }


public PreparedStatement prepareStatement (Connection conn,String sql_code)
    throws SQLException {
prepstat = conn.prepareStatement(sql_code);
return prepstat;
}
} 

In my the second java class DB_Oracle_Insert_Statement i call dbconf.

package DB_Oracle_Connection;

import DB_Oracle_Connection.dbconf; 
import java.sql.Connection;
import java.sql.PreparedStatement;
public class DB_Oracle_Insert_Statement {

public static void main(String[] args) {    
try {

 dbconf connect = new dbconf();

 // create our java preparedstatement using a sql update query
 PreparedStatement ps = connect.prepareStatement((Connection) connect, "INSERT INTO MKR_TEST1       VALUES ( ?, ?, ? , ?)");

 System.out.format("INSERT MKR_TEST1 VORNAME, NACHNAME, ADRESSE\n");
 // set the preparedstatement parameters
 ps.setString(1,"zzzzzVORNAME");
 ps.setString(2,"zzzzzNACHNAME");
 ps.setString(3,"zzzzADRESSE");
 ps.setString(4,"11");

 // call executeUpdate to execute our sql update statement
 ps.executeUpdate();

 //System.out.format("COMMIT\n");

 //PreparedStatement ps1 = connect.prepareStatement(
   //      "COMMIT");
 // call executeUpdate to execute our commit statement
 //ps1.executeUpdate();

 ps.close();
}
catch (Exception e)
{
  System.err.println("Got an exception! ");
  System.err.println(e.getMessage());
}


}


}

My compiler eclpise says everything is fine with my code. But when I execute my code Got an exception!

class DB_Oracle_Connection.dbconf cannot be cast to class java.sql.Connection (DB_Oracle_Connection.dbconf is in module DB_Oracle_Connection of loader 'app'; java.sql.Connection is in module java.sql of loader 'platform')

How do I fix problem in my code ?

3
  • 1
    Take care of java naming conventions.Class names should start with upper case character Commented Jan 23, 2020 at 10:22
  • 3
    (Connection) connect - no, you can't do that. connect is of type dbconf, not Connection and the two are in no way compatible with each other. Commented Jan 23, 2020 at 10:25
  • What is the benefit of even having your own prepareStatement method? I would remove that method entirely. Commented Jan 23, 2020 at 14:15

2 Answers 2

2

Your connectis not a class which implements the Connection interface which is expected by the prepared statement.

So change connect to connect.getConnection()

PreparedStatement ps = connect.prepareStatement(connect.getConnection(), "INSERT INTO MKR_TEST1       VALUES ( ?, ?, ? , ?)");

or use your private variable in your prepared statement method:

public PreparedStatement prepareStatement (String sql_code)
    throws SQLException {
    prepstat = connection.prepareStatement(sql_code);
    return prepstat;
}

BTW: Take care of java naming conventions. Class names should start with upper case character

Sign up to request clarification or add additional context in comments.

Comments

0

your class cannot be casted to a Connection class. It is not in the inheritance hierarchy. So to get the connection object correctly, just call your method getConnection() in this way in the class DB_Oracle_Insert_Statement

...
public static void main(String[] args) {    
try {

 dbconf connect = new dbconf();

 // create our java preparedstatement using a sql update query
 PreparedStatement ps = connect.prepareStatement(connect.getConnection(), "INSERT INTO MKR_TEST1       VALUES ( ?, ?, ? , ?)");
...

1 Comment

Hello, thank you for your help. I have renamed my connection class to DBConf. And I changed the PreparedStatement ps = connect.prepareStatement(connect.getConnection(), "INSERT INTO MKR_TEST1 VALUES ( ?, ?, ? , ?)");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.