Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication1;
- import com.sun.rowset.CachedRowSetImpl;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.sql.rowset.CachedRowSet;
- /**
- *
- * @author student
- */
- public class JavaApplication1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws SQLException {
- // Create a variable for the connection string.
- String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
- "databaseName=NORTHWND;user=maciej;password=maciekmaciek";
- // Declare the JDBC objects.
- Connection con = null;
- //Statement stmt = null;
- PreparedStatement stmt = null;
- ResultSet rs = null;
- CachedRowSet crs = new CachedRowSetImpl();
- 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);
- */
- String SQL = "SELECT * FROM [Customers] WHERE Country = ?";
- stmt = con.prepareStatement(SQL);
- stmt.setString(1, "UK");
- rs = stmt.executeQuery();
- crs.populate(rs);
- con.close();
- // Iterate through the data in the result set and display it.
- /*
- while (rs.next()) {
- System.out.println(rs.getString("ContactName"));
- }
- */
- while (crs.next()) {
- System.out.println(crs.getString("ContactName"));
- }
- }
- // 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) {}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement