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 lab4pp;
- import java.sql.*;
- /**
- *
- * @author student
- */
- public class Lab4PP {
- /**
- * @param args the command line arguments
- */
- static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- static final String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=NORTHWIND;";
- static final String USER = "benek";
- static final String PASS = "password";
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName(JDBC_DRIVER);
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- String Category = "Employees";
- String FirstText= "FirstName";
- String SecondText = "LastName";
- sql = "SELECT FROM ";
- try (ResultSet rs = stmt.executeQuery(sql)) {
- while (rs.next()) {
- System.out.println(rs.getString(4) + " " + rs.getString(6));
- }
- }
- stmt.close();
- conn.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- } catch(Exception e){
- e.printStackTrace();
- }
- finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Goodbye!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement