JDBC Practice Programs
JDBC Practice Programs
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306","root","");
stmt.execute(query);
System.out.println("Database created");
catch(Exception e)
System.out.println(e);
}
Create Table
import java.sql.*;
class CreateTable{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql = "create table Student1(Sno int(4), Sname varchar(30), Address varchar(30))";
stmt.executeUpdate(sql);
con.close();
catch(Exception e)
System.out.println(e.getMessage());
class Insert{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
System.out.println("Inserted successfully...");
stmt.executeUpdate(sql);
System.out.println("Inserted successfully...");
con.close();
}catch(Exception e)
{ System.out.println(e);
import java.sql.*;
import java.io.*;
class Insert_n{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
do{
System.out.println("enter id:");
int id=Integer.parseInt(br.readLine());
System.out.println("enter name:");
String name=br.readLine();
System.out.println("enter Address:");
String address=br.readLine();
ps.setInt(1,id);
ps.setString(2,name);
ps.setString(3,address);
int i=ps.executeUpdate();
String s=br.readLine();
if(s.startsWith("n")){
break;
}
}while(true);
con.close();
catch(Exception e){
System.out.println(e);}
import java.sql.*;
class Select{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
//stmt.executeUpdate(sql);
while(rs.next())
con.close();
}catch(Exception e){ System.out.println(e);}
Update Data
import java.sql.*;
class Update{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
while(rs.next())
con.close();
import java.sql.*;
class AddColumn{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
while(rs.next())
con.close();
Alter(Delete Column)
import java.sql.*;
class DropColumn{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
while(rs.next())
con.close();
import java.sql.*;
class DeleteRow{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
con.close();
import java.sql.*;
class DeleteAllRows{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
while(rs.next())
con.close();
}
Drop Table
import java.sql.*;
class DropTable{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
con.close();