0% found this document useful (0 votes)
7 views7 pages

JDBC 1

The document provides an overview of Java Database Connectivity (JDBC) and demonstrates how to perform CRUD operations using MySQL. It includes examples of creating a database, inserting, updating, and deleting records, as well as executing queries through Java code. The document outlines the steps for establishing a JDBC connection and executing SQL commands in a Java program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

JDBC 1

The document provides an overview of Java Database Connectivity (JDBC) and demonstrates how to perform CRUD operations using MySQL. It includes examples of creating a database, inserting, updating, and deleting records, as well as executing queries through Java code. The document outlines the steps for establishing a JDBC connection and executing SQL commands in a Java program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

JDBC Connection

---------------------------------
Java Database Connectivity.

Database?
------------------
--->collection of related Data.

Database software------>SQL,Oracle,MangoDB,Cloud,....

mysql

CRUD

C--->create
R--->Read
U--->Update
D--->Delete

w3school.com --->sql

Ex:
-------
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.35 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| besant |
| database1 |
| database10 |
| database11 |
| database12 |
| database13 |
| database14 |
| database15 |
| database16 |
| database2 |
| database3 |
| database4 |
| database5 |
| database6 |
| database7 |
| database8 |
| database9 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
21 rows in set (36.13 sec)

mysql> create database database17;


Query OK, 1 row affected (3.84 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| besant |
| database1 |
| database10 |
| database11 |
| database12 |
| database13 |
| database14 |
| database15 |
| database16 |
| database17 |
| database2 |
| database3 |
| database4 |
| database5 |
| database6 |
| database7 |
| database8 |
| database9 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
22 rows in set (0.83 sec)

mysql> use database17;


Database changed
mysql> show tables;
Empty set (2.84 sec)

mysql> create table Besant(sno int(6),name varchar(15),age int(10),city


varchar(20));
Query OK, 0 rows affected, 2 warnings (19.30 sec)

mysql> insert into Besant values(101,"Lotus",26,"Chennai");


Query OK, 1 row affected (1.25 sec)

mysql> insert into Besant values(102,"Jasmine",15,"Chennai");


Query OK, 1 row affected (0.36 sec)

mysql> select * from besant;


+------+---------+------+---------+
| sno | name | age | city |
+------+---------+------+---------+
| 101 | Lotus | 26 | Chennai |
| 102 | Jasmine | 15 | Chennai |
+------+---------+------+---------+
2 rows in set (0.00 sec)

mysql> insert into Besant values(103,"Vijay",35,"Banglore");


Query OK, 1 row affected (0.10 sec)

mysql> insert into Besant values(104,"Sathish",26,"CBE"),


(105,"Sathya",22,"Banglore");
Query OK, 2 rows affected (0.20 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from besant;


+------+---------+------+----------+
| sno | name | age | city |
+------+---------+------+----------+
| 101 | Lotus | 26 | Chennai |
| 102 | Jasmine | 15 | Chennai |
| 103 | Vijay | 35 | Banglore |
| 104 | Sathish | 26 | CBE |
| 105 | Sathya | 22 | Banglore |
+------+---------+------+----------+
5 rows in set (0.02 sec)

mysql> update besant set city="CBE" where name="jasmine";


Query OK, 1 row affected (1.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from besant;


+------+---------+------+----------+
| sno | name | age | city |
+------+---------+------+----------+
| 101 | Lotus | 26 | Chennai |
| 102 | Jasmine | 15 | CBE |
| 103 | Vijay | 35 | Banglore |
| 104 | Sathish | 26 | CBE |
| 105 | Sathya | 22 | Banglore |
+------+---------+------+----------+
5 rows in set (0.00 sec)

mysql> delete from besant where city="Banglore";


Query OK, 2 rows affected (0.35 sec)

mysql> select * from besant;


+------+---------+------+---------+
| sno | name | age | city |
+------+---------+------+---------+
| 101 | Lotus | 26 | Chennai |
| 102 | Jasmine | 15 | CBE |
| 104 | Sathish | 26 | CBE |
+------+---------+------+---------+
3 rows in set (0.00 sec)

mysql>
-----------------------------------------------------------------------------------
-----
JDBC Steps:
----------------------
1)import package
2)load and register the driver class
3)connecting the database
4)writing a query
5)execute the query
6)processing the result
7)closing the connection.

EX:
-----
package JDBC_Connection;

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

public class JDBC_Connect {

public static void main(String[] args)


{
try
{
//step:1 load the driver class

Class.forName("com.mysql.cj.jdbc.Driver");

//step:2 create the connection object

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database17","root","ro
ot");

//create statement object

Statement st=con.createStatement();

//execute the query

//ResultSet rs=st.executeQuery("select * from besant");

ResultSet rs=st.executeQuery("select * from besant");

rs.next();

System.out.println("Sno is "+rs.getInt(1));

System.out.println("Name is "+rs.getString(2));

System.out.println("Age is "+rs.getInt(3));

System.out.println("City is "+rs.getString(4));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
------------------------------------------------------
package JDBC_Connection;

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

public class JDBC_Connect {

public static void main(String[] args)


{
try
{
//step:1 load the driver class

Class.forName("com.mysql.cj.jdbc.Driver");

//step:2 create the connection object

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database17","root","ro
ot");

//create statement object

Statement st=con.createStatement();

//execute the query

//ResultSet rs=st.executeQuery("select * from besant");

ResultSet rs=st.executeQuery("select * from besant");

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getInt(3)+rs.getString(4));
}
}
catch(Exception e)
{
System.out.println(e);
}
}

}
-----------------------------------------------------------------------------------
----------------
update the data
------------------------------
package JDBC_Connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBC_Connect {

public static void main(String[] args)


{
try
{
//step:1 load the driver class

Class.forName("com.mysql.cj.jdbc.Driver");

//step:2 create the connection object

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database17","root","ro
ot");

//create statement object

Statement st=con.createStatement();

//execute the query

//ResultSet rs=st.executeQuery("select * from besant");

int x =st.executeUpdate("update besant set city=Banglore where


name=Lotus");

System.out.println("Data Updates successfully!!!");


}
catch(Exception e)
{
System.out.println(e);
}
}

You might also like