Skip to content
Snippets Groups Projects
Commit ddc34297 authored by Clemens Kupke's avatar Clemens Kupke
Browse files

lab1monday

parent 9e117ced
No related branches found
No related tags found
No related merge requests found
/* prompt the user for a value */
\prompt 'Enter the lowest value for employee number!' enumber
/* execute the query on the database */
SELECT TO_CHAR ( current_date, 'DD/Mon/YY' ) TODAY ,
EMPNO, DEPTNO, ENAME, SAL
FROM EMP
WHERE EMPNO >= (:enumber);
CREATE OR REPLACE FUNCTION add_salaries()
RETURNS emp.sal%TYPE AS
$$
DECLARE
salary emp.sal%TYPE;
maxsal emp.sal%TYPE;
c_emp CURSOR FOR select sal from emp where empno >= 7700;
BEGIN
maxsal = 0;
OPEN c_emp;
LOOP
FETCH c_emp INTO salary;
EXIT WHEN NOT FOUND;
maxsal = GREATEST(total,maxsal);
END LOOP;
CLOSE c_emp;
RETURN total;
END;
$$
LANGUAGE 'plpgsql';
import java.net.URL;
import java.sql.*;
public class lab13
{
static final int iMaxNumRows = 100;
Connection con;
Driver driver = null;
CallableStatement cstmt;
ResultSet result;
PreparedStatement pstmt;
static String name = new String("");
static String sUserPwd = new String("");
static String sDatabase = new String("");
// EMP table columns
static int aiEMEmpNo[] = new int[iMaxNumRows];
static String asEMEName[] = new String[iMaxNumRows];
static int aiEMDeptNo[] = new int[iMaxNumRows];
static int afEMSal[] = new int[iMaxNumRows];
// counter for number of rows in each table
int iNumDept = 0;
int iNumSalGrade = 0;
int iNumEmp = 0;
private void connect(String sTypeJDBC) throws Exception {
String url;
try {
// Create a new JDBC connection. This is our session.
url = "jdbc:postgresql://devweb2018.cis.strath.ac.uk:5432/postgres";
con = DriverManager.getConnection(url,name,sUserPwd);
con.setAutoCommit(false);
} catch(Exception e) {
System.err.println("System Exception in connect");
e.printStackTrace();
throw e;
}
}
public void closeConnection() throws Exception
{
try {
con.close();
} catch (Exception e) {
System.err.println("System Exception in closeConnection");
e.printStackTrace();
}
}
public String listEmp () throws Exception{
String retVal = new String("NUMBER\tNAME\tDEPTNO\tSALARY\n");
try {
pstmt = con.prepareStatement(
"SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP");
result = pstmt.executeQuery();
int i = 0;
while (result.next()) {
aiEMEmpNo[i] = result.getInt("EMPNO");
asEMEName[i] = result.getString("ENAME");
afEMSal[i] = result.getInt("SAL");
aiEMDeptNo[i] = result.getInt("DEPTNO");
retVal = retVal + aiEMEmpNo[i] + "\t" +
asEMEName[i] + "\t" + aiEMDeptNo[i] + "\t" +
afEMSal[i] + "\n";
i++;
}
iNumEmp = i;
pstmt.close();
}
catch (Exception e) {
System.err.println("System Exception in listEmp");
e.printStackTrace();
throw e;
}
return(retVal);
}
public static String runQuery (String sTypeJDBC, String sTable)
{
String retVal = new String();
lab13 myQuery = new lab13();
try {
myQuery.connect(sTypeJDBC);
retVal = myQuery.listEmp();
myQuery.closeConnection();
}
catch (Exception e) {
System.err.println("System Exception in runQuery");
e.printStackTrace();
retVal = "System Exception in runQuery";
}
return(retVal);
}
public static void main (String argv[])
{
String sTable = new String();
if ( argv.length == 2 ) {
name = argv[0];
sUserPwd = argv[1];
//sDatabase = argv[1];
System.out.println(runQuery("client", sTable));
}
else {
System.out.println("Usage: java lab13 <username> <password>");
}
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%@ page
import = "java.sql.*"
%>
<title>JSP Example </title></head><body><table>
<%
Class.forName("org.postgresql.Driver");
Connection dbconn = DriverManager.getConnection( "jdbc:postgresql://devweb2018.cis.strath.ac.uk:5432/postgres","<USERNAME>","<PASSWORD>");
Statement sql = dbconn.createStatement();
ResultSet results = sql.executeQuery("SELECT ename FROM emp where empno >= 7700");
while(results.next())
{
out.println("<tr><td>");
out.println(results.getString(1));
out.println("</td></tr>");
}
%>
</table></body></html>
CREATE OR REPLACE FUNCTION add_population() RETURNS <ENTER> AS
$$
DECLARE
popul <ENTER>;
total <ENTER>;
c_pop CURSOR FOR <ENTER>;
BEGIN
total = 0;
OPEN c_pop;
LOOP
FETCH c_pop INTO popul;
EXIT WHEN NOT FOUND;
total = total + popul;
END LOOP;
CLOSE c_pop;
RETURN total;
END;
$$
LANGUAGE 'plpgsql';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment