Set UP
Download the driver.
public static Connection connect() {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return null;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@dbip/name:1521:$$", "user", "pw");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return null;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
return connection;
}
public static void updateRecord(String flag,String id) throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String updateTableSQL = "update tablename"
+ " SET name = '"+flag+"' "
+ "WHERE id = ''"+id+"";
try {
dbConnection = connect();
statement = dbConnection.createStatement();
System.out.println(updateTableSQL);
// execute update SQL stetement
statement.execute(updateTableSQL);
System.out.println("Record is updated to tablename table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
//Returns the data
public static Data selectRecordsFromTable() throws SQLException {
Connection dbConnection = null;
Statement statement = null;
//create your own data class to save the result
Data data = new Data();
String selectTableSQL = "select query";
try {
dbConnection = connect();
statement = dbConnection.createStatement();
System.out.println(selectTableSQL);
// execute select SQL stetement
ResultSet rs = statement.executeQuery(selectTableSQL);
while (rs.next()) {
data.id= rs.getString("id");
data.name= rs.getString("name");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return data;
}
//Data calss
public class Data {
public static String id ;
public static String name ;
}