Collection of tasks on programming . Old version

 

 by Aliaksandr Prykhodzka

 

xsl, programming language, class, declaration, applet, education, servlet, examples
 

 

 

Java. Nest J.21. Working with databases

 

 


J.21.1. We have database containing table Tab1. This table has 2 columns Id and XX of type "integer number". Table contains 3 rows with values (Id=1, XX= 100), (Id=2, XX=22), (Id=3, XX=55). Also we have ODBC-source "Access1" for this database. And we have console application connecting to database by bridge JDBC-ODBC. Text of this console application follows. Determine a sequence of numbers produced by this console application. (This task is tested on MS Access database).


J.21.2. Description of task is the same as in J.21.1.

 

J.21.3. Description of task is the same as in J.21.1.

 

J.21.4. Description of task is the same as in J.21.1.

 

J.21.5. We have database from task J.21.1. Determine a sequence of numbers produced by the following JSP-application.

 

J.21.6. We have database from task J.21.1. The following servlet produces HTML-page with sequence of numbers. Determine this sequence of numbers.

 

 


J.21.1. We have database containing table Tab1. This table has 2 columns Id and XX of type "integer number". Table contains 3 rows with values (Id=1, XX= 100), (Id=2, XX=22), (Id=3, XX=55). Also we have ODBC-source "Access1" for this database. And we have console application connecting to database by bridge JDBC-ODBC. Text of this console application follows. Determine a sequence of numbers produced by this console application. (This task is tested on MS Access database).


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


public class test_base_1 {

public static void main(String[] Args) throws Exception {

String url = "jdbc:odbc:Access1";
String query = "SELECT Id, XX FROM Tabl1";

int jj = 0;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

boolean pr = rs.next();
while (pr) {
        for (int i = 1; i <= 2; i++) {
                jj = rs.getInt(i);
                System.out.println(Integer.toString(jj));
        }       
        pr = rs.next();
}

rs.close();
stmt.close();
con.close();
}
}

Answers

 


J.21.2. Description of task is the same as in J.21.1.


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


public class test_base_2 {

public static void main(String[] Args) throws Exception {

String url = "jdbc:odbc:Access1";
String query1 = "delete from Tabl1";
String query2 = "insert into Tabl1 (Id, XX) values (1, 3)";
String query3 = "insert into Tabl1 (Id, XX) values (2, 5)";
String query4 = "insert into Tabl1 (Id, XX) values (3, 10)";
String queryX = "SELECT Id, XX FROM Tabl1";

int jj = 0;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(query3);
stmt.executeUpdate(query4);

ResultSet rs = stmt.executeQuery(queryX);

boolean pr = rs.next();
while (pr) {
        for (int i = 1; i <= 2; i++) {
                jj = rs.getInt(i);
                System.out.println(Integer.toString(jj));
       }       
        pr = rs.next();
}

rs.close();

stmt.close();
con.close();
}
}

Answers

 


J.21.3. Description of task is the same as in J.21.1.


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


public class test_base_3 {

public static void main(String[] Args) throws Exception {

String url = "jdbc:odbc:Access1";
String query1 = "delete from Tabl1";
String query2 = "insert into Tabl1 (Id, XX) values (1, 3)";
String query3 = "insert into Tabl1 (Id, XX) values (2, 5)";
String query4 = "insert into Tabl1 (Id, XX) values (3, 10)";
String queryX = "SELECT Id, XX FROM Tabl1";

int jj = 0;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(query3);
stmt.executeUpdate(query4);

ResultSet rs = stmt.executeQuery(queryX);

boolean pr = rs.next();
while (pr) {
        jj+= rs.getInt(2);
        pr = rs.next();
}

System.out.println(Integer.toString(jj));

rs.close();

stmt.close();
con.close();
}
}

Answers

 


J.21.4. Description of task is the same as in J.21.1.


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


public class test_base_4 {

public static void main(String[] Args) throws Exception {

String url = "jdbc:odbc:Access1";
String query1 = "delete from Tabl1";
String query2 = "insert into Tabl1 (Id, XX) values (1, 3)";
String query3 = "insert into Tabl1 (Id, XX) values (2, 5)";
String query4 = "insert into Tabl1 (Id, XX) values (3, 10)";
String queryX = "SELECT Id, XX FROM Tabl1";

int jj = 0;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(query3);
stmt.executeUpdate(query4);

ResultSet rs = stmt.executeQuery(queryX);

boolean pr = rs.next();
while (pr) {
        for (int i = 1; i <= 2; i++) {
                jj+= rs.getInt(i);
        }
        pr = rs.next();
}

System.out.println(Integer.toString(jj));

rs.close();

stmt.close();
con.close();
}
}

Answers

 


J.21.5. ÈWe have database from task J.21.1. Determine a sequence of numbers produced by the following JSP-application.


File jsp_A.jsp

<%@page contentType="text/html; charset=windows-1251"%>
<html>
<head>
<title>jsp_A</title>
</head>
<jsp:useBean id="jsp_ABeanId" scope="session" class="pr_a.Jsp_ABean"/>
<jsp:setProperty name="jsp_ABeanId" property="*"/>
<body bgcolor="#ffffff">
<form method="post" action="jsp_A.jsp">
<jsp:getProperty name="jsp_ABeanId" property="sample"/>
<br>
<br>
<input type="submit" name="Submit" value="Submit">
<br>
</form>
</body>
</html>


File Jsp_ABean.java

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

public class Jsp_ABean {
private int jj = 0;
private boolean pr = false;
private ResultSet rs;

private void St() throws Exception {
        String url = "jdbc:odbc:Access1";
        String query = "SELECT Id, XX FROM Tabl1";

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection(url, "", "");
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(query);

        pr = rs.next();
}

public Jsp_ABean() throws Exception {
        St();
}


public String getSample() throws Exception {
        if (pr) {
                jj = rs.getInt(2);
                pr = rs.next();
       }
       else jj=0;

       return Integer.toString(jj);
}

public void setSample(String newValue) {
}
}

Answers

 


J.21.6. We have database from task J.21.1. The following servlet produces HTML-page with sequence of numbers. Determine this sequence of numbers.


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class Servlet_6 extends HttpServlet {
private static final String CONTENT_TYPE =
"text/html; charset=windows-1251";
private int jj = 0;
private boolean pr = false;
private ResultSet rs;

public void init() throws ServletException {
String url = "jdbc:odbc:Access1";
String query = "SELECT Id, XX FROM Tabl1";

try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection(url, "", "");
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(query);

        pr = rs.next();
} catch (Exception ex) { }
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);

if (pr) {
        try {
                jj+= rs.getInt(2);
                pr = rs.next();
        } catch (SQLException ex) { }
}

String st = Integer.toString(jj);

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet_6</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<form method='post'>");
out.println(st);
out.println("<br><br>");
out.println("<input type='submit' name='Submit' value='Submit'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

public void destroy() {
}
}

Answers

 

 

©   Aliaksandr Prykhodzka    1993 - 2007