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开发者_JAVA技巧 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)
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
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).
Working with JDBC requires several steps. Usually they are:
- Create a connection;
- Create a statement (or prepared statement);
- Execute the statement;
- Process the results;
- 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.
精彩评论