follow the following steps:
1: load the database driver
2: Stablish a connection between your program and your database
3: Create an SQL statement
4: Execute it
5: if the execution returns a result set then access your date returned by the database through the resuleset.
6: close the connection.
try this out
import java.sql.*;
public class ConnectinTest {
public void getValue() throws SQLException,ClassNotFoundException{
String str = "select name from username";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(str);
while(rs.next()) {
System.out.println("name: "+rs.getString(1));
}
}
public static void main(String args[]) throws SQLException,ClassNotFoundException {
new ConnectinTest().getValue();
}
No comments:
Post a Comment