Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc;
- import java.sql.*;
- import java.util.Scanner;
- import com.sun.rowset.CachedRowSetImpl;
- public class source {
- static void zad1() {
- System.out.println("||ZAD 1||");
- String connectionUrl = "jdbc:sqlserver://localhost:1433;"
- + "databaseName=Northwind;user=Laionik;password=lech";
- // Declare the JDBC objects.
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Establish the connection.
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection(connectionUrl);
- // Create and execute an SQL statement that returns some data.
- String SQL = "SELECT * FROM Customers";
- stmt = con.createStatement();
- rs = stmt.executeQuery(SQL);
- // Iterate through the data in the result set and display it.
- while (rs.next()) {
- System.out.println(rs.getString("CompanyName") + ": "
- + rs.getString("City") + " " + rs.getString("Country"));
- }
- }
- // Handle any errors that may have occurred.
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (rs != null)
- try {
- rs.close();
- } catch (Exception e) {
- }
- if (stmt != null)
- try {
- stmt.close();
- } catch (Exception e) {
- }
- if (con != null)
- try {
- con.close();
- } catch (Exception e) {
- }
- }
- }
- @SuppressWarnings("resource")
- static void zad2() {
- System.out.println("\n\n||ZAD 2||");
- String connectionUrl = "jdbc:sqlserver://localhost:1433;"
- + "databaseName=Northwind;user=Laionik;password=lech";
- Scanner in = new Scanner(System.in);
- // Declare the JDBC objects.
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Establish the connection.
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection(connectionUrl);
- // Create and execute an SQL statement that returns some data.
- System.out.println("Podaj kraj");
- String SQL = "SELECT * FROM Customers WHERE Country = ?";
- String param = in.nextLine();
- PreparedStatement preparedStatement = con.prepareStatement(SQL);
- preparedStatement.setString(1, param);
- rs = preparedStatement.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getString("CompanyName") + ": "
- + rs.getString("City") + " " + rs.getString("Country"));
- }
- }
- // Handle any errors that may have occurred.
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (rs != null)
- try {
- rs.close();
- } catch (Exception e) {
- }
- if (stmt != null)
- try {
- stmt.close();
- } catch (Exception e) {
- }
- if (con != null)
- try {
- con.close();
- } catch (Exception e) {
- }
- }
- }
- @SuppressWarnings("resource")
- static void zad3() {
- System.out.println("\n\n||ZAD 3||");
- try
- {
- CachedRowSetImpl crs = new CachedRowSetImpl();
- crs.setUsername("Laionik");
- crs.setPassword("lech");
- crs.setUrl("jdbc:sqlserver://localhost:1433;databaseName=Northwind;");
- crs.setCommand("SELECT * FROM Customers");
- crs.execute();
- while (crs.next()) {
- System.out.println(crs.getString("CompanyName") + ": "
- + crs.getString("City") + " "
- + crs.getString("Country"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // Declare the JDBC objects.
- zad1();
- zad2();
- zad3();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement