You are on page 1of 8

Assignment 5: JDBC

NOTE: create 3 separate java files for the 3 parts below and upload them separately (you can
submit 3 files).

1. Write a Java program which connects to PostgreSQL University


database. Use whatever user name you to create the relations, when
connecting from JDBC. If you followed the instructions correctly,
the relations should have been created in a schema with the same
name as the user name, and should be accessible from JDBC.
1. The program should take the student id as a commandline
argument and display the list of courses the student has
enrolled for.
2. Show appropriate error messages with exception handling.
This can be quite useful also for debugging your program.

//Program:1
import java.io.*;
import java.sql.*;

public class MyData {


public static void main(String[] args) throws IOException{

try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC
driver!");
System.exit(1);
}

String hostname = "localhost";


String username = "khushboo";
String password = "khushboo123";
String dbName = "university";
String connectionUrl = "jdbc:postgresql://" +
hostname + "/" + dbName;
Connection conn = null;

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);
}

} catch (SQLException sqlerr) {


System.out.println(sqlerr);
System.exit(1);
}
}
}
/* Write a Java program which uses the Railway schema you created earlier to
do the following:
3. Accept the following 3 arguments <stcode1 stcode2
distance> and either insert or update the track relation. i.e.,
insert if no tuple with stcode1 and stcode2 exists, else update
the distance.

Program:2 */

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

public class MyData {


public static void main(String[] args) throws
IOException{

try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC
driver!");
System.exit(1);
}

String hostname = "localhost";


String username = "khushboo";
String password = "khushboo123";
String dbName = "railway";
String connectionUrl = "jdbc:postgresql://" +
hostname + "/" + dbName;
Connection conn = null;

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();

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter Station Code#2:");
String stcode2 = br.readLine();

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter Distance:");
Int distance = 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();
}

} catch (SQLException sqlerr) {


System.out.println(sqlerr);
System.exit(1);
}
}
}
2. Write a Java program which does the following:
1. Takes as first line of input, a parametrized SQL query, and
subsequent lines containing values for the parameters; you
can assume that each ? in the SQL query viewed as a string
represents a parameter value.
2. Execute the parametrized SQL query, and output its result
with one line per row in the result, along with a header
containing the names of columns. Use the tab character to
separate columns
3. Note: You can assume that what is input is a query, not an
update. Count the number of ? characters in the query string
to figure out the number of parameters. Don't worry about
datatypes, just use setString() to set parameter values, and
getString() to get resultset attribute values.

Program :
import java.io.*;
import java.sql.*;

public class MyData {


public static void main(String[] args) throws IOException{

try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC driver!");
System.exit(1);
}

String hostname = "localhost";


String username = "khushboo";
String password = "khushboo123";
String dbName = "university";
String connectionUrl = "jdbc:postgresql://" + hostname + "/" +
dbName;
Connection conn = null;

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 );
}

} catch (SQLException sqlerr) {


System.out.println(sqlerr);
System.exit(1);
}
}
}

You might also like