JDBC Program - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

JDBC Program

JDBC Program 


How to Connect to Oracle Database in Java Program? 


1) create new Java project file

2) select created Java project and add JDBC driver, from external jar location.

this external jar location available inside configure build path.









after adding jar file(ojdbc6) downloaded from internet almost our  setup is ready. now we can communicate with Database(oracle).   




3) write Java code inside src Java file 


package com.smarttechguides;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class AxisBank {
// data base details(connection url, user name, password details)
static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
static String userName = "system";
static String password = "manager";
public static void main(String[] args) throws Exception {
// establish connection
Connection con = DriverManager.getConnection(url, userName, password);
Scanner sc = new Scanner(System.in);
System.out.println("enter customer id");
int cid = sc.nextInt();
// sql query for fetching records from database
String query = "select *from axisbank where customerid=" + cid;
// create statement
Statement statement = con.createStatement();
// executing query
ResultSet result = statement.executeQuery(query);
while (result.next()) {
int id = result.getInt(1);
String name = result.getString(2);
double total = result.getDouble(5);
System.out.println(
"customer id" + " " + id + "\n" + "customer name" + " " + name + "\n" + "total balance " + total);
}
// close all connections
result.close();
statement.close();
con.close();
}
}
view raw AxisBank.java hosted with ❤ by GitHub


Create table and insert records into table




 

4) Run Java application and see result on the console because we are using system.out.print() method.

Results will be fetching from database.

 

No comments:

Post a Comment