JDBC Solutions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1.

Write a JDBC Program in java to display the names of all


Students studying in BCA

import java.io.IOException;
import java.sql.*;

public class Student


{
public static void main(String[] args) throws SQLException,
ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:DSN");
Statement st = conn.createStatement();
String sql = "Select * from Student where Programme like 'BCA'";
ResultSet rs = st.executeQuery(sql);

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

}
}

2. Write a JDBC program to delete Write a JDBC Program in java to


delete employees with salary>80000, and display the names of
all employees where salary<30000
import java.io.IOException;
import java.sql.*;

public class Student_del


{
public static void main(String[] args) throws SQLException,
ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:DSN");
Statement st = conn.createStatement();
String sql = "delete from Employee where Salary>80000";
st.executeUpdate(sql);

sql = "Select * from Employee where Salary<30000";


ResultSet rs= st.executeQuery(sql);

Prof. Monika Gadre


while(rs.next()){
System.out.println("\n"+
rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));

}
}
}

3. Write a JDBC Program in java to display the names of all


Employees
import java.io.IOException;
import java.sql.*;

public class Employee


{
public static void main(String[] args) throws SQLException,
ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:DSN");
Statement st = conn.createStatement();
String sql = "Select * from Employee";
ResultSet rs = st.executeQuery(sql);

while(rs.next()){
System.out.println("\n"+
rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}

}
}

4. Write a JDBC Program in java to update salary of employees


whose salary is less than Rs.50000 and display all the records
of Employee table
import java.io.IOException;
import java.sql.*;

public class Employee


{
public static void main(String[] args) throws SQLException,
ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Prof. Monika Gadre


Connection conn = DriverManager.getConnection("jdbc:odbc:DSN");
Statement st = conn.createStatement();
String sql = "Update Employee set Salary=80000 where
Salary<50000";
st.executeUpdate(sql);

sql = "Select * from Employee";


ResultSet rs= st.executeQuery(sql); ;

while(rs.next()){
System.out.println("\n"+
rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}

}
}

Prof. Monika Gadre

You might also like