Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to write a SQL query in Java

801497Mar 7 2009 — edited Mar 7 2009
Hi, I'm writing a program that pulls all data from the database with a simple "SELECT * FROM [TABLE NAME]. The problem is that I don't know how to set it up properly. My code will only connect to the database but not execute the query. Please help.



{code}import java.sql.*;
import java.util.Properties;
import java.sql.*;

public class DatabaseSelect {

public static void main(String[] args) {
System.out.println("Connected to the database!");
Connection conn = null;
String url = "jdbc:oracle://localhost:1571/";
String dbName = "jdbc";
String driver = "com.oracle.jdbc.Driver";
String userName = "HR";
String password = "database";
String query = "SELECT * FROM HR";

try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");

} catch (Exception e) {
e.printStackTrace();
}
}
}{code}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2009
Added on Mar 7 2009
5 comments
1,473 views