Advertisement
Guest User

LAB 4 pp

a guest
Oct 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package lab4pp;
  8.  
  9. import java.sql.*;
  10.  
  11.  
  12. /**
  13. *
  14. * @author student
  15. */
  16. public class Lab4PP {
  17.  
  18. /**
  19. * @param args the command line arguments
  20. */
  21. static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  22. static final String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=NORTHWIND;";
  23. static final String USER = "benek";
  24. static final String PASS = "password";
  25.  
  26. public static void main(String[] args) {
  27. Connection conn = null;
  28. Statement stmt = null;
  29.  
  30. try {
  31. Class.forName(JDBC_DRIVER);
  32.  
  33. System.out.println("Connecting to database...");
  34. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  35.  
  36. System.out.println("Creating statement...");
  37. stmt = conn.createStatement();
  38. String sql;
  39. String Category = "Employees";
  40. String FirstText= "FirstName";
  41. String SecondText = "LastName";
  42. sql = "SELECT FROM ";
  43. try (ResultSet rs = stmt.executeQuery(sql)) {
  44.  
  45. while (rs.next()) {
  46. System.out.println(rs.getString(4) + " " + rs.getString(6));
  47. }
  48. }
  49. stmt.close();
  50. conn.close();
  51.  
  52.  
  53.  
  54. } catch (SQLException ex) {
  55. ex.printStackTrace();
  56. } catch(Exception e){
  57. e.printStackTrace();
  58. }
  59. finally{
  60. //finally block used to close resources
  61. try{
  62. if(stmt!=null)
  63. stmt.close();
  64. }catch(SQLException se2){
  65. }// nothing we can do
  66. try{
  67. if(conn!=null)
  68. conn.close();
  69. }catch(SQLException se){
  70. se.printStackTrace();
  71. }//end finally try
  72. }//end try
  73. System.out.println("Goodbye!");
  74.  
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement