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

Practical No 8

The document contains 4 Java programs that demonstrate basic operations with JDBC and MySQL: 1) A program to display data from a table by connecting to a database and executing a SELECT query. 2) A program to insert a record into a table by executing an INSERT query. 3) A program to update a record in a table by executing an UPDATE query. 4) A program to delete a record from a table by executing a DELETE query.

Uploaded by

Ansh Gala
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)
24 views

Practical No 8

The document contains 4 Java programs that demonstrate basic operations with JDBC and MySQL: 1) A program to display data from a table by connecting to a database and executing a SELECT query. 2) A program to insert a record into a table by executing an INSERT query. 3) A program to update a record in a table by executing an UPDATE query. 4) A program to delete a record from a table by executing a DELETE query.

Uploaded by

Ansh Gala
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

a.

Write a JDBC program that displays the data of a given table


package database;

import java.sql.*;

public class display


{
public static void main(String s[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rup",
"root","applet");
Statement st=con.createStatement();
String qry="select * from student";
ResultSet rs=st.executeQuery(qry);

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

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

b. Write a JDBC program to insert a record in table


package shraddha;
import java.sql.*;
public class Insert
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rup1","root","applet");

Statement st=con.createStatement();

String qry="insert into student values(1,'Gauri')";


st.executeUpdate(qry);
con.close();
System.out.println("data inserted");
}
catch(Exception e)
{
System.out.println(e);
}
}

c.Write a JDBC program to update a record in table

package shraddha;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class update


{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rup1","root","applet");

Statement st=con.createStatement();

String qry="update student set name='damini' where id=7112";


st.executeUpdate(qry);

con.close();
System.out.println("data updated");
}
catch(Exception e)
{
System.out.println(e);
}
}

d.Write a JDBC program to delete a record in table

package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class delete


{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rup", "root", "applet");

Statement st=con.createStatement();

String qry="delete from student where name='shraddha'";


st.executeUpdate(qry);

con.close();

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

You might also like