JDBC Programs
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateDatabase{
public static void main(String args[]) throws SQLException {
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306","root","");
Statement stmt = con.createStatement();
//Query to create a database
String query = "CREATE database VVIT";
//Executing the query
stmt.execute(query);
System.out.println("Database created");
catch(Exception e)
System.out.println(e);
}
Create Table
import java.sql.*;
class CreateTable{
public static void main(String args[]){
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);
System.out.println("Tabele created successfully...");
con.close();
catch(Exception e)
System.out.println(e.getMessage());
Insert Data (Static)
import java.sql.*;
class Insert{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql = "insert into Student1 values(511,'Rajesh','tenali')";
stmt.executeUpdate(sql);
System.out.println("Inserted successfully...");
sql = "insert into Student1 values(520,'Rajesh','Hyd')";
stmt.executeUpdate(sql);
System.out.println("Inserted successfully...");
con.close();
}catch(Exception e)
{ System.out.println(e);
Insert Data Dynamically N records
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","");
PreparedStatement ps=con.prepareStatement("insert into student1 values(?,?,?)");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
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();
System.out.println(i+" records affected");
System.out.println("Do you want to continue: y/n");
String s=br.readLine();
if(s.startsWith("n")){
break;
}
}while(true);
con.close();
catch(Exception e){
System.out.println(e);}
Retrieve Data And Display
import java.sql.*;
class Select{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
//String sql="delete from student1";
//stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
Update Data
import java.sql.*;
class Update{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="update Student1 set Sname='Jeevan' where Sno=501";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
Alter (Add Column)
import java.sql.*;
class AddColumn{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="alter table student1 add(Section varchar(30))";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
Alter(Delete Column)
import java.sql.*;
class DropColumn{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="alter table student1 drop column Section";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
Delete Row from table
import java.sql.*;
class DeleteRow{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="delete from Student1 where Sno=501";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
Delete All Rows from table(Table will exist)
import java.sql.*;
class DeleteAllRows{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="delete from Student1";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from student1");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
Drop Table
import java.sql.*;
class DropTable{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/VVIT","root","");
Statement stmt=con.createStatement();
String sql="drop Table Student1";
stmt.executeUpdate(sql);
con.close();
}catch(Exception e){ System.out.println(e);}