JAVA DATABASE CONNECTIVITY
package jdbc;

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

/**
* This interface has all the constants for making JDBC Connections
*
* @author Darur Venugopal
*
*/
public interface IJDBCConstants {
// In Interface all the variables are public , static and final
String host = "jdbc:oracle:thin:@localhost:1521:xe";
String username = "HR";
String password = "ORACLE";
String driverName = "oracle.jdbc.driver.OracleDriver";
// Declare methods , In interface just method signature are defined.
Connection makeConnection();

Statement makeStatement();

// ResultSet getresultSet();
void LoadDriver();
}
How to connect to a database using java: How to select from database using java
package jdbc;

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

public class PerfectJdbc {

private static final String sqlQuery = "select email ,first_name, last_name from employees";

public static void main(String[] args) {
     System.out.println("STEP 1 is LOAD JAVA DRIVER IN THIS RUNNONG ENVIRONMENT");
     IJDBCConstants jdbcImplementor =
new JDBCImplementor();

// valid instantiation

// step 1
     jdbcImplementor.LoadDriver();

System.out.println("STEP 2 IS TO MAKE CONNECTION FROM JAVA TO DATABASE");
Connection connection = jdbcImplementor.makeConnection();

Statement statement;

try {
statement = connection.createStatement();

// step 3
ResultSet resultSet = statement.executeQuery(

sqlQuery); // step4

int i = 0;

// now we got the result set, print the values from
// result set.

//

while (resultSet.next()) {

     System.out.println("----------------:" + (++i)+ "START :----------------");
     System.out.println("First Name: \t\t\t"+ resultSet.getString("first_name"));
     System.out.println("last Name: \t\t\t"+ resultSet.getString("last_name"));
     System.out.println("Email: \t\t\t\t" + resultSet.getString("email"));
     System.out.println("----------------:" + (i)+ " end:----------------");
}

}

catch (SQLException e) {
     System.err.println("SQLEXCEPTION: " + e);
}

}

}
How to insert data into databases like oracle, MySQL, SQLSERVER using java
package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLInsertion {
private static final String sqlQuery =

"select email ,first_name, last_name from employees";

private static final String sqlQueryForsql ="select sino, name,place ,email from sqltable";

// insertion statement

private static final String insertQuery ="INSERT INTO SQLTABLE VALUES ( ? , ? , ?,? ) ";

public static void main(String[] args) {
System.out.println("STEP 1 is LOAD JAVA DRIVER IN THIS RUNNONG ENVIRONMENT");
IJDBCConstants jdbcImplementor =new JDBCImplementor();

// valid instantiation
// step 1
jdbcImplementor.LoadDriver();

System.out.println("STEP 2 IS TO MAKE CONNECTION FROM JAVA TO DATABASE");
Connection connection = jdbcImplementor.makeConnection();

Statement statement;
try {

statement = connection.createStatement();
// step 3
ResultSet resultSet = statement.executeQuery(sqlQuery); // step4

int i = 0;

// now that I got the result set i want to print the values from
// result set.

//

while (resultSet.next()) {
String name =resultSet.getString("first_name") +" " + resultSet.getString("last_name");
String email = resultSet.getString("email");
String place ="NIZAMABAD";

PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);

// For Insertions USe Prepared Insertions
preparedStatement.clearParameters();

preparedStatement.setString(1, name);

preparedStatement.setString(2, place);

preparedStatement.setInt(3, ++i);

preparedStatement.setString(4, email);

preparedStatement.executeUpdate();

}

ResultSet resultSetForsql = jdbcImplementor.makeStatement().executeQuery(sqlQueryForsql);

while (resultSetForsql.next()) {
System.out.println("11111");
String name =resultSetForsql.getString("name");
String email =resultSetForsql.getString("email");
String place =resultSetForsql.getString("place");

int siNo = resultSetForsql.getInt("sino");
String result = siNo +" " + name + " " + email;
System.out.println(result);
}

}

catch (SQLException e) {
System.err.println("SQLEXCEPTION: " + e);
}

}

}
Now we see the output of this programs.
package jdbc;

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

public class JDBCImplementor implements IJDBCConstants {

public static Connection connection = null;

public static Statement statement = null;

public void LoadDriver() {

// CHecked Exceptions

try {
Class.forName(driverName);
System.out.println("DRIVER LOADED SUCCESSFULLY");
}

catch (ClassNotFoundException classNotFound) {
System.out.println("CLASS NOT FOUND EXCEPTION " + classNotFound);
}

}

public Connection makeConnection() {

try {

connection = DriverManager.getConnection(host, username, password);
System.out.println("Connected properly ");
}

catch (SQLException sqlException) {
System.out.println("SQLEXCEPTION: " + sqlException);
}

return connection;

}

public Statement makeStatement() {

if (connection != null) {

try {

statement = connection.createStatement();
}

catch (SQLException sqlException) {
System.out.println("SQLEXCEPTION: " + sqlException);
}

}

return statement;

}

}
 

 

www.iforerunner.com