0% found this document useful (0 votes)
14 views

Sample Programs on JDBC

Uploaded by

bhavani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sample Programs on JDBC

Uploaded by

bhavani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Prerequisite

1. You should have MySQL on your System.


2. Netbeans IDE(
3. You should have JDK on your System.
3. To set up the connectivity, the user should have MySQL
Connector to the Java (JAR file:
https://downloads.mysql.com/archives/c-j/)

Click Below link to install MySQL


https://dev.mysql.com/downloads/installer/

Steps:

1.Create a database by DataScience


2.In that database create a table by name
student(Columns:usn,name,dob,age,department)

1.Connecting to database

import java.io.*;
import java.sql.*;

class Demo{
public static void main(String[] args) throws Exception
{
String url=
"jdbc:mysql://localhost:3306/DataScience"; // table details
String username = "root"; // MySQL credentials
String password = "system123";
String query = "select *from student"; // query to be
run
Class.forName("com.mysql.cj.jdbc.Driver"); // Driver
name
Connection con = DriverManager.getConnection( url,
username, password);
System.out.println("Connection Established
successfully");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query); // Execute
query
rs.next();
String name
= rs.getString("name"); // Retrieve name from db

System.out.println(name); // Print result on console


st.close(); // close statement
con.close(); // close connection
System.out.println("Connection Closed....");
}
}

2. Java JDBC program to create table

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

public class Demo {


static final String DB_URL =
"jdbc:mysql://localhost:3306/DataScience";
static final String USER = "root";
static final String PASS = "system123";

public static void main(String[] args) {


// Open a connection
try(Connection conn =
DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
) {
String sql = "CREATE TABLE REGISTRATION " +
"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";

stmt.executeUpdate(sql);
System.out.println("Created table in given
database...");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Output:
Created table in given database...

3. Inserting records to database

import java.sql.*;
public class Demo {
static final String DB_URL =
"jdbc:mysql://localhost:3306/DataScience";
static final String USER = "root";
static final String PASS = "system123";

public static void main(String[] args) {


// Open a connection
try{
Connection conn = DriverManager.getConnection(DB_URL,
USER, PASS);
Statement stmt = conn.createStatement();

// Execute a query
System.out.println("Inserting records into the
table...");
String sql = "INSERT INTO student VALUES (111,
'Zara', '2024-6-28', 18,'BE')";
stmt.executeUpdate(sql);
sql = "INSERT INTO student VALUES (222, 'Zara',
'2024-6-28', 19,'BE')";
stmt.executeUpdate(sql);
sql = "INSERT INTO student VALUES (333, 'Zara',
'2024-6-28', 18,'MCA')";
stmt.executeUpdate(sql);

System.out.println("Inserted records into the


table...");
}
catch (SQLException e)
{
e.printStackTrace();
}

4. Java JDBC program to display table details.

import java.sql.*;

class Demo{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/
datascience","root","system123");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
111 Zara 2024-06-28
222 Zara 2024-06-28
333 Zara 2024-06-28

5. Java JDBC program to delete records

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

public class Demo {


static final String DB_URL =
"jdbc:mysql://localhost/datascience";
static final String USER = "root";
static final String PASS = "system123";
static final String QUERY = "SELECT
usn,name,dob,age,department FROM student";

public static void main(String[] args) {


// Open a connection
try(Connection conn =
DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
) {
String sql = "DELETE FROM student " +
"WHERE usn = 222";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);
while(rs.next()){
//Display values
System.out.print("USN: " + rs.getInt("usn"));
System.out.print(", NAME: " +
rs.getString("name"));
System.out.print(", DOB: " +
rs.getString("dob"));
System.out.println(", AGE: " + rs.getInt("age"));
System.out.println(", DEPT: " +
rs.getString("department"));
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

USN: 333, NAME: Zara, DOB: 2024-06-28, AGE: 18


, DEPT: MCA USN: 333, NAME: Zara, DOB: 2024-06-28, AGE: 18
, DEPT:

You might also like