124697
124697

Reputation: 21893

how do I send parameters as a string

I'm trying to do the following:

String dbURL= "jdbc:oracle:thin:@HOST:PORT:DB,USERNAME,PASSWORD";
//Statement stmt = conn.createStatement(dbURL);

conn.createStatment is expecting 3 parameters, not just 1 string so i get an error. is it possible to have them all in one string and pass it? I know it's not the right way to go about this particular problem but i'm asking for future reference as well.

Edit: my mistake. i mean

Connection conn = DriverManager.getConnection(URL)

Upvotes: 0

Views: 230

Answers (3)

Eli Acherkan
Eli Acherkan

Reputation: 6411

Working with JDBC requires several steps. Usually they are:

  1. Create a connection;
  2. Create a statement (or prepared statement);
  3. Execute the statement;
  4. Process the results;
  5. Close the resources (result set, statement, connection).

I have a feeling you're not sure about JDBC basics. Take a look at the JDBC tutorial.

Upvotes: 0

darioo
darioo

Reputation: 47183

As others have pointed out, what you are doing just won't work. Use this:

String dbURL= "jdbc:oracle:thin:@HOST:PORT:DB,USERNAME,PASSWORD";
Connection conn = DriverManager.getConnection(dbURL);
PreparedStatement ps = conn.prepareStatement("your SQL");

The first line is your database URL.

The second line will get the actual connection using the db URL.

The third line will let you get the prepared statement from your connection.

This pattern is widely used, so you should use it too (unless you're in Java EE, where obtaining db connections works a bit differently).

Upvotes: 2

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

Use PreparedStatement instead. It is supposed to get SQL query as a parameter. Your connection string is supposed to be passed to DriverManager.getConnection() method

Upvotes: 3

Related Questions