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

Java unit 6 (Programs)

The document contains Java code examples for basic database operations using JDBC, including creating a table, inserting, updating, and deleting records. It also demonstrates the use of PreparedStatement for dynamic input and the ResultSet interface for retrieving data from a database. Each code snippet connects to a MySQL database and performs the specified operations while handling exceptions.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java unit 6 (Programs)

The document contains Java code examples for basic database operations using JDBC, including creating a table, inserting, updating, and deleting records. It also demonstrates the use of PreparedStatement for dynamic input and the ResultSet interface for retrieving data from a database. Each code snippet connects to a MySQL database and performs the specified operations while handling exceptions.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java unit 6 (Programs) :

1.​ Create Table

package myfirstproject;
import java.sql.*;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ String url = "jdbc:mysql://localhost:3306/mydatabase";
​ ​ String user = "root";
​ ​ String pass = "root";
​ ​ String create = "CREATE TABLE student(id INT, name VARCHAR(10))";
​ ​ try
​ ​ {
​ ​ ​ Connection c = DriverManager.getConnection(url,user,pass);
​ ​ ​ Statement st = c.createStatement();
​ ​ ​ st.execute(create);
​ ​ ​ c.close();
​ ​ }
​ ​ catch(Exception e)
​ ​ {
​ ​ ​ System.out.println(e.getMessage());
​ ​ }
​ }
}

2.​ Insert , Update , Delete :

import java.sql.*;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ String url = "jdbc:mysql://localhost:3306/mydatabase";
​ ​ String user = "root";
​ ​ String pass = "root";
​ ​ String create = "CREATE TABLE student(name VARCHAR(20), id INT, marks INT)";
​ ​ String insert = "INSERT INTO student VALUES('ram',101,80)";
​ ​ String update = "UPDATE student SET name = 'Krsihna' WHERE id = 101";
​ ​ String delete = "DELETE FROM student WHERE id = 101";
​ ​ try
​ ​ {
​ ​ ​ Connection c = DriverManager.getConnection(url,user,pass);
​ ​ ​ Statement s = c.createStatement();
​ ​ ​ s.executeUpdate(create);
​ ​ ​ s.executeUpdate(insert);
​ ​ ​ System.out.println("Inserted Successfully");
​ ​ ​ s.executeUpdate(update);
​ ​ ​ System.out.println("Updated Successfully");
​ ​ ​ s.executeUpdate(delete);
​ ​ ​ System.out.println("Deleted Successfully");​
​ ​ }
​ ​ catch(Exception e)
​ ​ {
​ ​ ​ System.out.println(e.getMessage());
​ ​ }
​ }
}

3.​ PreparedStatement : (Dynamic Input)

package myfirstproject;
import java.sql.*;
import java.util.*;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ Scanner sc = new Scanner(System.in);
​ ​ String url = "jdbc:mysql://localhost:3306/mydatabase";
​ ​ String user = "root";
​ ​ String pass = "root";
​ ​ String insert = "INSERT INTO student(id,name) VALUES(?,?)";
​ ​ String name;
​ ​ int id;
​ ​ System.out.println("Enter id & name : ");
​ ​ id=sc.nextInt();
​ ​ name = sc.next();
​ ​ try
​ ​ {
​ ​ ​ Connection c = DriverManager.getConnection(url,user,pass);
​ ​ ​ PreparedStatement ps = c.prepareStatement(insert);
​ ​ ​ ps.setInt(1, id);
​ ​ ​ ps.setString(2, name);
​ ​ ​ ps.executeUpdate();
​ ​ ​ System.out.println("Inserted SUcessfully");
​ ​ ​ c.close();
​ ​ }
​ ​ catch(Exception e)
​ ​ {
​ ​ ​ System.out.println(e.getMessage());
​ ​ }
​ }
}

4.​ Result Set interface (Retriving data):

package myfirstproject;
import java.sql.*;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ String url = "jdbc:mysql://localhost:3306/mydatabase";
​ ​ String user = "root";
​ ​ String pass = "root";
​ ​ String select = "SELECT * FROM student";
​ ​ try
​ ​ {
​ ​ ​ Connection c = DriverManager.getConnection(url,user,pass);
​ ​ ​ Statement s = c.createStatement();
​ ​ ​ ResultSet rs = s.executeQuery(select);
​ ​ ​ System.out.println("Data From Student Table : ");
​ ​ ​ while(rs.next())
​ ​ ​ {
​ ​ ​ ​ int id = rs.getInt("id");
​ ​ ​ ​ String name = rs.getString("name");
​ ​ ​ ​ System.out.println("Id : "+id);
​ ​ ​ ​ System.out.println("Name : "+name);
​ ​ ​ }
​ ​ ​ c.close();
​ ​ }
​ ​ catch(Exception e)
​ ​ {
​ ​ ​ System.out.println(e.getMessage());
​ ​ }
​ }
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy