/////////////////// PROJECT CourseEgov /////////////////// ////////////////////////////////////////////// //////// PACKAGE courseEgovPack ////////////////////////////////////////////// package courseEgovPack; import javax.jws.WebMethod; import javax.jws.WebService; @WebService // compiler directive to turn this into a web service public class NumberService { // sumUpProcedure // String mInput: value to sum up 1..mInput (typed web services not possible) // return value as String (typed web services not possible) @WebMethod // compiler directive to turn this into a web method public String sumUpProcedure (String mInput){ // return sum of 1..mInput int m=Integer.valueOf(mInput); int x=0; // accumulator variable for (int i=1;i<=m;i++){ x=x+i; } return String.valueOf(x); } } ////////////////////////////////////////////// //////// PACKAGE coursePlainJavaPack ////////////////////////////////////////////// package coursePlainJavaPack; public class SumOfNumbers { public static void main (String[] args) { //int n=Integer.valueOf(args[0]); // get the number from the Console int n = 10; // program puts out Sum of numbers 1...n int x=0; // accumulator variable for the result value for (int i=1; i<=n; i++){ x = x + i; } System.out.println("Our calculation:" + x); System.out.println("Gauss calculation:" + ((n*(n+1))/2)); } } /////////////////////////////////////////////////////////////////////////// package coursePlainJavaPack; public class NumberAgent { // OBJECT-ORIENTED PROGRAMMING // attributes of NumberAgent objects private int n=0; // methods of NumberAgent objects NumberAgent(){ // Constructor } public void setValue(int x){ n=x; } public int getValue(){ return n; } public void sumUp(){ // computes and stores sum of 1..n int x=0; // accumulator variable for (int i=1;i<=n;i++){ x=x+i; } n=x; } public void factorial(){ // returns sum of 1..n int x=1; // accumulator variable for (int i=1;i<=n;i++){ x=x*i; } n=x; } // END: OBJECT-ORIENTED PROGRAMMING // a main method to exploit NumberAgent objects public static void main (String[] args){ NumberAgent numberAgent= new NumberAgent(); numberAgent.setValue(10); numberAgent.sumUp(); System.out.println("Sum 1...10 :" + numberAgent.getValue()); numberAgent.setValue(4); numberAgent.factorial(); System.out.println("Fac of 4 :" + numberAgent.getValue()); System.out.println("Sum 1...10 :" + sumUpProcedure(10)); System.out.println("Fac of 4 :" + factorialProcedure(4)); System.out.println("Sum 1...10 :" + sumUpFunction(10)); System.out.println("Fac of 4 :" + factorialFunction(4)); } // STRUCTURED PROGRAMMING (a.k.a PROCEDURAL PROGRAMMING) private static int sumUpProcedure (int m){ // return sum of 1..m int x=0; // accumulator variable for (int i=1;i<=m;i++){ x=x+i; } return x; } private static int factorialProcedure (int m){ // return factorial of m int x=1; // accumulator variable for (int i=1;i<=m;i++){ x=x*i; } return x; } // FUNCTIONAL PROGRAMMING private static int sumUpFunction (int m){ // return sum of 1..m if (m>1){ return m + sumUpFunction(m-1); } else { return 1; } } private static int factorialFunction (int m){ // return factorial m if (m>1){ return m * factorialFunction(m-1); } else { return 1; } } } /////////////////////////////////////////////////////////////////////////// package coursePlainJavaPack; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class BasicDBAccessLocal { public static void main(String args[]){ Connection c = null; try { String url = "jdbc:postgresql://localhost:5432/postgres"; String username = "postgres"; String password = "ICT637postgresql"; Class.forName("org.postgresql.Driver"); c = DriverManager.getConnection(url, username, password); c.setAutoCommit(false); Statement selectStatement = c.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY ); // READ DATA ResultSet resultSet = selectStatement.executeQuery( "Select * from company;" ); resultSet.first(); System.out.println(resultSet.getString(2)); while (resultSet.next()){ System.out.println(resultSet.getString(2)); } // WRITE DATA resultSet.last(); System.out.println( "Number of rows: " + String.valueOf(resultSet.getRow()) ); selectStatement.executeUpdate( "INSERT INTO company (id,name,age,address,salary)" + "VALUES (" + String.valueOf(resultSet.getRow()+1) + "," + "'bob', 30, 'testaddress', 1500" + ");" ); selectStatement.close(); c.commit(); c.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getClass().getName() +": " + e.getMessage()); System.exit(0); } } } /////////////////////////////////////////////////////////////////////////// package coursePlainJavaPack; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class BasicDBAccessCloud { public static void main(String args[]){ Connection c = null; try { String url = "jdbc:postgresql://babar.elephantsql.com:5432/qqwtblyv"; //Todo: String username = "qqwtblyv"; String password = "ALo8tyV9bxRKkXm6691WELtjdYbX8L4p"; Class.forName("org.postgresql.Driver"); c = DriverManager.getConnection(url, username, password); c.setAutoCommit(false); Statement selectStatement = c.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY ); // READ DATA ResultSet resultSet = selectStatement.executeQuery( "Select * from testdata;" ); resultSet.first(); System.out.println(resultSet.getString(2)); while (resultSet.next()){ System.out.println(resultSet.getString(2)); } selectStatement.close(); c.commit(); c.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getClass().getName() +": " + e.getMessage()); System.exit(0); } } } /////////////////////////////////////////////////////////////////////////// package coursePlainJavaPack; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.NodeList; public class BasicSOAPAccess { public static void main(String args[]) { String n = "9"; // value of n, for which 1..n should be calculated try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody soapBody = envelope.getBody(); // XML element for method name SOAPElement soapBodyElemLevel1 = soapBody.addChildElement("sumUpProcedure"); // XML element for paramater name SOAPElement soapBodyElemLevel2 = soapBodyElemLevel1.addChildElement("mInput"); // setting value for paramater soapBodyElemLevel2.addTextNode(n); MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction",""); soapMessage.saveChanges(); // http://localhost:8080/'project name'/services/'web service name' String url = "http://localhost:8080/CourseEgov/services/NumberService"; SOAPMessage soapResponse = soapConnection.call(soapMessage, url); SOAPBody body = soapResponse.getSOAPBody(); // ns1 : default namespace NodeList nodeList = body.getElementsByTagName("ns1:sumUpProcedureReturn"); System.out.print("Sum of 1.." + n + " :"); System.out.println(nodeList.item(0).getTextContent()); //System.out.println(soapBody); //System.out.println(soapMessage); //soapMessage.writeTo(System.out); //printSOAPResponse(soapResponse); soapConnection.close(); } catch (Exception e) { System.err.println("Error"); e.printStackTrace(); } } } ////////////////////////////////////////////// //////// Java Sever Pages :: RequestPage.jsp ////////////////////////////////////////////// <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>