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

ajp_prac_19

Uploaded by

saeedarwatkar
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)
15 views

ajp_prac_19

Uploaded by

saeedarwatkar
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/ 5

Practical No.

19

1) Program to create a connection.

package MyPrject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class connectionex2 {

public static void main(String[] args) {

try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Scanner sc=new Scanner(System.in);
PreparedStatement st=con.prepareStatement("Create table Employee2(Emp_name
varchar(20), salary Integer)");
st.executeUpdate();
System.out.println("Table created");

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

Output:
2) Program to create table and insert row in it.
package MyPrject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class connectionex2 {

public static void main(String[] args) {

try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Scanner sc=new Scanner(System.in);
PreparedStatement st=con.prepareStatement("Create table Employee3(Emp_name
varchar(20), salary Integer)");
st.executeUpdate();
System.out.println("Table created");
PreparedStatement st2=con.prepareStatement("insert into Employee3 values(?,?)");
st2.setString(1, "Saee");
st2.setInt(2, 200000);
int l=st2.executeUpdate();
System.out.println("records Inserted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
3) Program to update row in the table.
package MyPrject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;

public class connectionex2 {

public static void main(String[] args) {

try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Statement sc=con.createStatement();
Statement su=con.createStatement();
su.executeUpdate("Update Employee3 set salary=100000 where Emp_name='saee'");
System.out.println("Updation SUccessfull...");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
4) Program to retrieve data using ResultSet.
package MyPrject;

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

public class resultsetEx {

public static void main(String[] args) {

try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Statement sc=con.createStatement();
Statement si=con.createStatement();
Statement ss=con.createStatement();
sc.executeUpdate("create table Student(Name varchar(30),percentage Integer)");
System.out.println("Table Created...");
si.executeUpdate("insert into Student values('Saee',98)");
si.executeUpdate("insert into Student values('Soham',90)");
si.executeUpdate("insert into Student values('Samrat',97)");
si.executeUpdate("insert into Student values('Manisha',94)");
String sql="select * from Student";
ResultSet rs=ss.executeQuery(sql);
rs=ss.getResultSet();
while(rs.next())
{
System.out.println(rs.getString("Name")+" "+rs.getInt("percentage"));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}

}
Output:
5) Program to update a record in a database table.
package MyPrject;

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

public class coonectionEx3 {

public static void main(String[] args) {

try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Statement su=con.createStatement();

System.out.println("Table Created...");
su.executeUpdate("update Student set percentage=100 where Name='Saee'");
System.out.println("Updation Successful in Student table...");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}

}
Output:

You might also like