Friday, July 19, 2013

Read data from database and display result using JAVA coding



import java.sql.*;

public class JavaMySQL {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "sys as sysdba";
String password = "welcome1";
ResultSet result = null;

String sql = "SELECT DETAILS From SERVER";

try {
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
//System.out.println(statement.execute(sql));
statement.execute(sql);
result= statement.getResultSet();
while(result.next() ){
String val = result.getString(1);
System.out.println(val);}
connection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
}


No comments: