Java JDBC Connection Oracle Execute Simple query Example:
Package com.test.dbtest;
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement; /**JDBC Connect Oracle Database Simple example * @author wanggq * @version Date Created: March 31, 2014 11:00:06 * Class description/public class TESTO_PROCEDURE01
{public static void main (string[] args) {String driver = ' oracle.jdbc.driver.OracleDriver ';
String url = "Jdbc:Oracle:thin: @localhost: 1521:ORCL";
Statement stmt = null;
ResultSet res = null;
Connection conn = null;
callablestatement proc = null;
String sql = "Select T.rec_no, t.airline,t.departure,t.arrival from Cdp_main_order T where T.departure=upper (' Pek ')";
try {class.forname (driver);
conn = drivermanager.getconnection (URL, "abc123", "abc123");
stmt = Conn.createstatement ();
res = stmt.executequery (SQL);
while (Res.next ()) {String rec = res.getstring ("Rec_no");
String airline = res.getstring ("Airline"); StRing Dept = res.getstring ("departure");
String arr = res.getstring ("Arrival");
System.out.println (rec+ "+airline+" "+dept+" "+arr);
The catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
You can also use the Prevent SQL injection PreparedStatement method
PreparedStatement stmt = null;
ResultSet res = null;
Connection conn = null;
callablestatement proc = null;
String sql = "Select T.rec_no, t.airline,t.departure,t.arrival from Cdp_main_order T where T.departure=upper (?)";
try {
class.forname (driver);
conn = drivermanager.getconnection (URL, "abc123", "abc123");
stmt = conn.preparestatement (sql);
Stmt.setstring (1, "Pek");
res = Stmt.executequery ();
Db: