/**
* Program for simple database connectivity and records fetching from table
*/
import java.sql.*;
public class dbCon {
public static void main(String arg[]) throws SQLException{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String name="";
try{
/*
* Java jdbc driver name -> oracle.jdbc.driver.OracleDriver
* Java url driver type -> jdbc:oracle:thin:
* Address -> @localhost "or" 120.15.87.153
* Port using Oracle -> 1521
* Database Name -> hbsp54db
* Username of Oracle -> tp2
* Password of Oracle -> tp2
*/
// Load the JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Create a connection to the database
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:hbsp54db","tp2","tp2");
}
catch(Exception e){
System.out.println("Unable to connect Database "+e);
}
String query = "Select username FROM users"; // use your table here
stmt = con.createStatement(); //Statment creation
rs = stmt.executeQuery(query); // resultset for executeQuery
while(rs.next()){ //checking value in next record from database
name= rs.getString(1); //geting username from user table
System.out.println(name);
}
con.close();
}
}
output:
~~~~~~~
balamurgan
krishna
sri
No comments:
Post a Comment