JDBC Connection
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
/**
* Created by IntelliJ IDEA.
* User: A130B
* Date: Jul 20, 2005
* Time: 2:39:47 PM
* To change this template use Options | File Templates.
*/
public class DbConnection {
public Connection getConnection() {
Connection con = null;
try {
// Class.forName(“com.mysql.jdbc.Driver”);
Class.forName(“oracle.jdbc.driver.OracleDriver”);
System.out.println(“driver berhasil”);
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
System.out.println(“Kesalahan driver: ” + e);
}
try {
con = DriverManager.getConnection(“jdbc:oracle:thin:@ENDERA-ORACLE:1521:orcl”, “system”, “manager”);
System.out.println(“connection berhasil”);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
System.out.println(“Kesalahan connection: ” + e);
}
return con;
}
}
=============================================================================================================================
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by IntelliJ IDEA.
* User: A130B
* Date: Jul 20, 2005
* Time: 2:42:24 PM
* To change this template use Options | File Templates.
*/
public class tesConDb {
public static void main (String[]args){
DbConnection tesCon = new DbConnection();
// tesCon.getConnection();
Connection con = tesCon.getConnection();
// tesCon.getConnection();
Statement stmt = null;
ResultSet rs = null;
String s = null;
String sqlString = new String();
sqlString = “select banner from v$version”;
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sqlString);
try {
while (rs.next()) {
// Get the data from the row using the column index
s = rs.getString(1);
System.out.println(“s = ” + s);
// // Get the data from the row using the column name
// s = rs.getString(“col_string”);
// }
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
}
=============================================================================================================================
String sql = “INSERT INTO T1 (ID) VALUES(?)”;
PreparedStatement pstmt = null;
pstmt = connection.prepareStatement(sql);
// Insert 10 rows
for (int i=0; i<10; i++) {
// Set the value
pstmt.setString(1, “row “+i);
// Insert the row
pstmt.executeUpdate();
Recent Comments