1package com.java2novice.jdbc;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6import java.sql.Statement;
7
8public class JdbcConnection {
9
10 public static void main(String a[]){
11
12 try {
13 Class.forName("oracle.jdbc.driver.OracleDriver");
14 Connection con = DriverManager.
15 getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
16 ,"user","password");
17 Statement stmt = con.createStatement();
18 System.out.println("Created DB Connection....");
19 } catch (ClassNotFoundException e) {
20 // TODO Auto-generated catch block
21 e.printStackTrace();
22 } catch (SQLException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26 }
27}
1Connection =
2 Helps our java project connect to database
3Statement =
4 Helps to write and execute SQL query
5ResultSet =
6 A DataStructure where the data from query result stored
7+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8
9
10 After succesfully created the connect
11next step is STATEMENT
12
13Statement statement = connection.createStatement();
14We use createStatement() method to create
15the statement from our connection.
16-The result we get from this type of statement
17can only move from top to bottom,
18not other way around
19
20Statement statement = connection.
21createStatement(ResultSet TYPE_SCROLL_INSENSITIVE
22,ResultSet CONCUR_READ_ONLY);
23
24-The result we get from this type of
25statement can freely move between rows
26
27Once we have statement we can run
28the query and get the result to
29ResultSet format
30 import java.sql.ResultSet;
31
32We use the method executeQuery()
33to execute our queries
34
35ResultSet result = statement.
36 executeQuery("Select * from employees");