Senin, November 14, 2011

HOW TO CONNECT ORACLE DATABASE IN JAVA (NETBEANS 6.9) #VERY SIMPLE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package oraclebaru;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
*
* @author Administrator
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Connection connection = null;
try {
String driverName = "oracle.jdbc.driver.OracleDriver"; //Default
Class.forName(driverName);

String serverName = "localhost"; //Default serverName, but you could change this with this '127.0.0.1'
String portNumber = "1521"; //Default portNumber
String sid = "orcl"; //Depending on your first installation settings
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String userName = "hr"; //username at login
String password = "hr"; //password at login
connection = DriverManager.getConnection(url, userName, password);

connection.setAutoCommit(false); //Dhurung ngerti fungsine nggo nopo
Statement st = connection.createStatement();

st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT LAST_NAME FROM employees"); //Action query
// System.out.println(rs.absolute(1)); //pass dicobo ora error, nanging muncul tulisan enkripsi sko resultSet dhewek
while(rs.next()){
System.out.println(rs.getString(1)); //Displaying String from rs 1 coloumn * LAST_NAME
}
rs.close();
st.close();
connection.close();
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
}
}

By : Laboran FTI
Referensi : http://in.answers.yahoo.com/question/index?qid=20101211005240AANg40Y
Good Luck..

Tidak ada komentar:

Posting Komentar