5 Steps to connect to the database in java
- 5 Steps to connect to the database in java
- Register the driver class
- Create the connection object
- Create the Statement object
- Execute the query
- Close the connection object
- 5 Steps to connect to the database in java
- Register the driver class
- Create the connection object
- Create the Statement object
- Execute the query
- Close the connection object
There are 5 steps to connect any java application with the database in java using JDBC. They are as ➝➝ |
Lets Describe Each Steps briefly :-
1) Register the driver class
| The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. |
Syntax of forName() method
Example to register the OracleDriver class
2) Create the connection object
| The getConnection() method of DriverManager class is used to establish connection with the database. |
Syntax of getConnection() method
Example to establish connection with the Oracle database
3) Create the Statement object
| The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database. |
Syntax of createStatement() method
Example to create the statement object
4) Execute the query
| The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. |
Syntax of executeQuery() method
Example to execute query
5) Close the connection object
| By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection. |
Syntax of close() method
Example to close connection
Read Me::
Here, i also made single insert and multiple insert in this code... You can also see only to insert single once.
class DBSql is a whole class which consits one part of code.
Function public void DB_con() prints all the records from the database(database name can be given as your choice) .. Then
public void DB_Insert() inserts the one set of data to the database as Mentioned which are the value input by the user.. and prints the new Updated data.... Last,
public void ins_Multiple() inserts the multiple data number of times inserts by the user...
My Suggestion is that to the viewers that: You shouldn't copy all the code and run it... Instead Of it please do code one by one and Understand the Basic Concept of JDBC...
The code Starts from here...
/*
* To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ncejava.dbsql;
import java.sql.*;
import java.util.Scanner;
/**
*
* @author sushantjava
*/
public class DBSql {
private Connection con = null;
private Statement st;
private ResultSet rs;
Scanner in = new Scanner(System.in);
public DBSql() {
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "Jdbc:mysql://localhost:3306/Database_b";
String username = "root";
String password = "";
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public void DB_con() {
try {
st = con.createStatement();
String sql = "SELECT * FROM Book";
rs = st.executeQuery(sql);
System.out.println("Records from Database");
System.out.println(" ID \t Name\t \t\t Author \t\t Price");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("Name");
String au_name = rs.getString("Author");
Double price = rs.getDouble("Prize");
System.out.println(id + "\t" + name + "\t" + au_name + "\t $" + price);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void DB_Insert() {
try {
System.out.println("Enter the Name of Book, Author Name , Prize of the book in dollar: ");
String book_Name = in.nextLine();
String name = in.nextLine();
Double price = in.nextDouble();
st = con.createStatement();
st.executeUpdate("INSERT INTO `Book` (Name,Author,Prize)values('" + book_Name + "', '" + name + "', '" + price + "' )");
System.out.println("The new data are:");
DB_con();
con.close();
} catch (Exception e) {
System.err.println("Got an exception!");
System.out.println(e);
}
}
public void ins_Multiple() {
System.out.println("Enter the number of data to insert:");
int n = in.nextInt();
System.out.println("Please Enter the " + n + "Datas:");
try {
for (int i = 1; i <= n; i++) {
System.out.println("For Data " + i);
System.out.println("Enter the Name of Book, Author Name, Prize of the Book in Dollar:");
String book_Name = in.nextLine();
String name = in.nextLine();
Double price = in.nextDouble();
st = con.createStatement();
st.executeUpdate("insert into Book(Name,Author,Prize)values('" + book_Name + "','" + name + "','" + price + "')");
}
System.out.println("The new data are:");
DB_con();
con.close();
} catch (Exception e) {
System.err.println("Got an Exception");
System.out.println(e);
}
}
}
The main class program is here...
package com.ncejava.dbsql;
import java.util.Scanner;
/**
*
* @author sushantjava
*/
public class DBCONNECT {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DBSql dbobj = new DBSql();
System.out.println("1.TO only see the all record!");
System.out.println("2.TO insert the one datas!");
System.out.println("3.TO insert the multiple datas!");
System.out.println("Please enter your choice!");
int choice = in.nextInt();
switch (choice) {
case 1:
dbobj.DB_con();
break;
case 2:
dbobj.DB_Insert();
break;
case 3:
dbobj.ins_Multiple();
break;
default:
System.out.println("Enter the correct choice!");
}
}
}

No comments:
Post a Comment