JDBC Statement
In JDBC, a statement object is responsible for sending the SQL statements to the database mean it is responsible to execute queries. Below i am providing the JDBC statement object implementation.
As I am using MySQL database for program execution so the table description with which i am dealing in the below examples is as below:-
CREATE TABLE student (
STUDENT_ID DECIMAL(20,0) DEFAULT NULL,
FIRSTNAME VARCHAR(50) NOT NULL,
LASTNAME VARCHAR(50) NOT NULL,
BIRTHDATE DATE DEFAULT NULL,
TOTAL_MARKS DECIMAL(20,0) DEFAULT NULL,
DIVISION VARCHAR(50) NOT NULL
)
For Data Insertion in the table the query is as below:-
INSERT INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES ('1','Kumar','Gaurav','1978-05-12','920','FIRST');
INSERT INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('2','Gaurav','Kumar','1980-11-19','880','FIRST');
INSERT INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('3','Shivam','Kumar','1989-07-11','670','SECOND');
Demo example of JDBC Statement( Providing configuration for both oracle and MySQL DB)
package com.gaurav.jdbcexamples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCStatementExample {
/** Configuraing a connection for Oracle 10G */
/*
private static String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver"; //Oracle Driver filename
private static String USERNAME = "system";
private static String PASSWORD = "system";
private static String DATABASE_URL = "jdbc:oracle:thin:@localhost:1521:XE"; //Database
connecting URL for Oracle
*/
/** Configuraing a connection for MySQL databse */
private static String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // MySQL Driver filename
private static String USERNAME = "root";
private static String PASSWORD = "root";
private static String DATABASE_URL = "jdbc:mysql://localhost:3306/test"; // Database connecting URL for MYSQL
public static void main(String args[]) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
String query = "select * from student";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
System.out.println("STUDENT_ID" + "\t" + "FIRSTNAME" + "\t"
+ "LASTNAME" + "\t" + "BIRTHDATE" + "\t" + "TOTAL_MARKS"
+ "\t" + "DIVISION");
while (rs.next()) {
System.out.println("\t"+rs.getInt(1) + "\t" + rs.getString(2) + "\t\t"
+ rs.getString(3) + "\t\t" + rs.getDate(4) + "\t"
+ rs.getInt(5) + "\t\t" + rs.getString(6));
}
} catch (SQLException sqe) {
sqe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
} catch (Exception e) {
e.getMessage();
}
}
}
}
Note:-
/**
* If we want to make a connection to Oracle database then remove the commented
* part at the top and make comment for MySQL configuration. For the Execution
* of the above program we need to add mysql-connector-java-3.1.12 or ojdbc14.jar
* in the classpath correspondingly depends on the configuration of the database
* through which we want to make connection. We can also connect through the DB which is
* available in different system using IP address like
* jdbc:oracle:thin:@<IP ADDRESS>:<PORT NUM>:<DB SCHEMANAME>
*/
Result
No comments:
Post a Comment