Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. package jdbc;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5.  
  6. import com.sun.rowset.CachedRowSetImpl;
  7.  
  8. public class source {
  9.  
  10.     static void zad1() {
  11.         System.out.println("||ZAD 1||");
  12.         String connectionUrl = "jdbc:sqlserver://localhost:1433;"
  13.                 + "databaseName=Northwind;user=Laionik;password=lech";
  14.  
  15.         // Declare the JDBC objects.
  16.         Connection con = null;
  17.         Statement stmt = null;
  18.         ResultSet rs = null;
  19.  
  20.         try {
  21.             // Establish the connection.
  22.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  23.             con = DriverManager.getConnection(connectionUrl);
  24.  
  25.             // Create and execute an SQL statement that returns some data.
  26.             String SQL = "SELECT * FROM Customers";
  27.             stmt = con.createStatement();
  28.             rs = stmt.executeQuery(SQL);
  29.  
  30.             // Iterate through the data in the result set and display it.
  31.             while (rs.next()) {
  32.                 System.out.println(rs.getString("CompanyName") + ": "
  33.                         + rs.getString("City") + " " + rs.getString("Country"));
  34.             }
  35.         }
  36.  
  37.         // Handle any errors that may have occurred.
  38.         catch (Exception e) {
  39.             e.printStackTrace();
  40.         } finally {
  41.             if (rs != null)
  42.                 try {
  43.                     rs.close();
  44.                 } catch (Exception e) {
  45.                 }
  46.             if (stmt != null)
  47.                 try {
  48.                     stmt.close();
  49.                 } catch (Exception e) {
  50.                 }
  51.             if (con != null)
  52.                 try {
  53.                     con.close();
  54.                 } catch (Exception e) {
  55.                 }
  56.         }
  57.     }
  58.  
  59.     @SuppressWarnings("resource")
  60.     static void zad2() {
  61.         System.out.println("\n\n||ZAD 2||");
  62.         String connectionUrl = "jdbc:sqlserver://localhost:1433;"
  63.                 + "databaseName=Northwind;user=Laionik;password=lech";
  64.         Scanner in = new Scanner(System.in);
  65.         // Declare the JDBC objects.
  66.         Connection con = null;
  67.         Statement stmt = null;
  68.         ResultSet rs = null;
  69.  
  70.         try {
  71.             // Establish the connection.
  72.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  73.             con = DriverManager.getConnection(connectionUrl);
  74.  
  75.             // Create and execute an SQL statement that returns some data.
  76.             System.out.println("Podaj kraj");
  77.  
  78.             String SQL = "SELECT * FROM Customers WHERE Country = ?";
  79.             String param = in.nextLine();
  80.             PreparedStatement preparedStatement = con.prepareStatement(SQL);
  81.             preparedStatement.setString(1, param);
  82.             rs = preparedStatement.executeQuery();
  83.             while (rs.next()) {
  84.                 System.out.println(rs.getString("CompanyName") + ": "
  85.                         + rs.getString("City") + " " + rs.getString("Country"));
  86.             }
  87.  
  88.         }
  89.  
  90.         // Handle any errors that may have occurred.
  91.         catch (Exception e) {
  92.             e.printStackTrace();
  93.         } finally {
  94.             if (rs != null)
  95.                 try {
  96.                     rs.close();
  97.                 } catch (Exception e) {
  98.                 }
  99.             if (stmt != null)
  100.                 try {
  101.                     stmt.close();
  102.                 } catch (Exception e) {
  103.                 }
  104.             if (con != null)
  105.                 try {
  106.                     con.close();
  107.                 } catch (Exception e) {
  108.                 }
  109.         }
  110.     }
  111.  
  112.     @SuppressWarnings("resource")
  113.     static void zad3() {
  114.         System.out.println("\n\n||ZAD 3||");
  115.         try
  116.         {
  117.             CachedRowSetImpl crs = new CachedRowSetImpl();
  118.              
  119.             crs.setUsername("Laionik");
  120.             crs.setPassword("lech");
  121.             crs.setUrl("jdbc:sqlserver://localhost:1433;databaseName=Northwind;");
  122.             crs.setCommand("SELECT * FROM Customers");
  123.             crs.execute();
  124.  
  125.             while (crs.next()) {
  126.                 System.out.println(crs.getString("CompanyName") + ": "
  127.                         + crs.getString("City") + " "
  128.                         + crs.getString("Country"));
  129.             }
  130.  
  131.         } catch (SQLException e) {
  132.             e.printStackTrace();
  133.         }
  134.  
  135.     }
  136.  
  137.     public static void main(String[] args) {
  138.         // TODO Auto-generated method stub
  139.         // Declare the JDBC objects.
  140.         zad1();
  141.         zad2();
  142.         zad3();
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement