Professional Documents
Culture Documents
NOTE: create 3 separate java files for the 3 parts below and upload them separately (you can
submit 3 files).
//Program:1
import java.io.*;
import java.sql.*;
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC
driver!");
System.exit(1);
}
try {
conn =
DriverManager.getConnection(connectionUrl,username,
password);
System.out.println("Connected successfullly");
} catch (SQLException sqlerr) {
System.out.println("Connection failed");
System.out.println(sqlerr);
sqlerr.printStackTrace();
System.exit(1);
}
try {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a Student ID:");
Int StudentID = br.readLine();
PreparedStatement
pstmt=conn.prepareStatement("select * from takes a, section b,
course c where a.course_id=b.course and a.sec_id=b.sec_id and
a.semester =b.semester and a.year=b.year
b.course_id=c.course_id and a.ID=?");
pstmt.setString(1, StudentID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String myTitle = rs.getString("title");
System.out.println(myTitle);
}
Program:2 */
import java.io.*;
import java.sql.*;
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC
driver!");
System.exit(1);
}
try {
conn =
DriverManager.getConnection(connectionUrl,username,
password);
System.out.println("Connected successfullly");
} catch (SQLException sqlerr) {
System.out.println("Connection failed");
System.out.println(sqlerr);
sqlerr.printStackTrace();
System.exit(1);
}
try {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Station Code#1:");
String stcode1 = br.readLine();
PreparedStatement
pstmt=conn.prepareStatement("select distance from track
where stcode1=? and stcode2=?");
pstmt.setString(1, stcode1);
pstmt.setString(2, stcode2);
ResultSet rs = pstmt.executeQuery();
if (rs.eof()) {
PreparedStatement
pstmt=conn.prepareStatement("insert into track values(?,?,?);
pstmt.setString(1, stcode1);
pstmt.setString(2, stcode2);
pstmt.setString(3, distance);
ResultSet rs = pstmt.executeQuery();
} else {
PreparedStatement
pstmt=conn.prepareStatement("update track set distance = ?
where stcode1=? and stcode2=?);
pstmt.setString(1, distance);
pstmt.setString(2, stcode1);
pstmt.setString(3, stcode2);
ResultSet rs = pstmt.executeQuery();
}
Program :
import java.io.*;
import java.sql.*;
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC driver!");
System.exit(1);
}
try {
conn = DriverManager.getConnection(connectionUrl,username,
password);
System.out.println("Connected successfullly");
} catch (SQLException sqlerr) {
System.out.println("Connection failed");
System.out.println(sqlerr);
sqlerr.printStackTrace();
System.exit(1);
}
try {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a Department Name:");
String DeptName = br.readLine();
PreparedStatement pstmt=conn.prepareStatement("select * from
instructor where dept_name=?");
pstmt.setString(1, DeptName);
ResultSet rs = pstmt.executeQuery();
System.out.println('Name' + /t + 'Salary');
while(rs.next()){
String myName = rs.getString("name");
String mySalary = rs.getString("salary");
System.out.println(myName + /t + mySalary );
}