ajp_prac_19
ajp_prac_19
19
package MyPrject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
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;
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;
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;
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;
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: