Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- ////////////////////////////
- public class EPR
- {
- //-----------------------------
- public static void main(String args[])throws Exception
- {
- try
- {
- String query1 = "select * from E";
- String query2 = "select * from P";
- String query3 = "select * from R";
- String query4 = "select * from EPR";
- String eid = "EID", ename = "ENAME", salary = "SALARY"; //columns of E table
- String pid = "PID", projname = "PROJECTNAME"; //columns of P table
- String rid = "RID", rolename = "ROLENAME"; //columns of R table
- System.out.println("Printing table E:\n");
- System.out.println(eid + " " + ename + " " + salary);
- print(query1, eid, ename, salary);
- System.out.println("Printing table P:\n");
- System.out.println(pid + " " + projname);
- print(query2, pid, projname, null);
- System.out.println("Printing table R:\n");
- System.out.println(rid + " " + rolename);
- print(query3, rid, rolename, null);
- System.out.println("Printing table EPR:\n");
- System.out.println(eid + " " + pid + " " + rid);
- print(query4, eid, pid, rid);
- String url = "jdbc:odbc:Employee-Project-Role";
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection(url);
- Statement stmt = con.createStatement();
- stmt.executeUpdate("insert into E values ('X555','Simons',44444)");
- //stmt.executeUpdate("insert into EPR values (select EID from E where EID = 'X555', select PID from P where PID = 23, select RID from R where RID = 22)");
- //stmt.executeUpdate("insert into EPR values (select EID = 'X555' from E, select PID = '23' from P, select RID = '22' from R)");
- stmt.executeUpdate("INSERT INTO EPR VALUES ('X555',14,41)");
- stmt.close();
- con.close();
- System.out.println("Printing table E:\n");
- System.out.println(eid + " " + ename + " " + salary);
- print(query1, eid, ename, salary);
- System.out.println("Printing table P:\n");
- System.out.println(pid + " " + projname);
- print(query2, pid, projname, null);
- System.out.println("Printing table R:\n");
- System.out.println(rid + " " + rolename);
- print(query3, rid, rolename, null);
- System.out.println("Printing table EPR:\n");
- System.out.println(eid + " " + pid + " " + rid);
- print(query4, eid, pid, rid);
- }catch(Exception ex)
- {
- System.out.println(ex);
- }
- }
- public static void print(String qury, String colmn1, String colmn2, String colmn3)throws Exception
- {
- String url = "jdbc:odbc:Employee-Project-Role";
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection(url);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(qury);
- while (rs.next())
- {
- String column = rs.getString(colmn1);
- System.out.print(column + " ");
- column = rs.getString(colmn2);
- System.out.print(column + " ");
- if(colmn3 != null)
- {
- column = rs.getString(colmn3);
- System.out.print(column + " \n");
- }
- else
- System.out.println("");
- }
- System.out.println("");
- rs.close();
- stmt.close();
- con.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement