Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.sql.*;
  3. public class PrintdepartmentEmps {
  4.  
  5. private static final String dbServer ="LAPTOP-3U8MO386\\SQLEXPRESS;"; //Insert your own server name here!
  6. private static final String dbDb = "Company;";
  7. private static String dbLoginUser = "sa;"; // Careful! Plain text username/PW
  8. private static String dbPassword = "system"; // Careful! Plain text username/PW
  9.  
  10. private static boolean isWindowsAuthentication = true; // Choose Windows/SQL Server Authentication here
  11.  
  12.  
  13. public static void main(String[] args) throws SQLException, InterruptedException {
  14.  
  15. String connectionString;
  16.  
  17. try {
  18. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  19. } catch (ClassNotFoundException driverNotFound) {
  20. System.out.println("Driver could not be loaded");
  21. }
  22.  
  23. if (isWindowsAuthentication) {
  24. connectionString = "jdbc:sqlserver://" + dbServer + "databaseName=" + dbDb + "integratedSecurity=true";
  25. }
  26. else {
  27. connectionString = "jdbc:sqlserver://" + dbServer + "databaseName=" + dbDb + "user=" + dbLoginUser + "password=" + dbPassword;
  28. }
  29.  
  30. Connection conn = DriverManager.getConnection(connectionString); // For now, the most interesting object
  31. //
  32. String lName;
  33. double salary;
  34. System.out.print("Hello DB World!\n");
  35. System.out.print(connectionString + "\n");
  36. System.out.print("Enter a Department number: ");
  37. Scanner sc = new Scanner(System.in);
  38. Integer dno = sc.nextInt();
  39. sc.close();
  40. String query = "select lname, salary from EMPLOYEE where dno = " + dno.toString();
  41. Statement statem = conn.createStatement();
  42. ResultSet rs = statem.executeQuery(query);
  43. System.out.println("\nData:");
  44. while (rs.next()) {
  45. lName = rs.getString(1);
  46. salary = rs.getDouble(2);
  47. System.out.println(lName + " " + (int)salary);
  48. }
  49. System.out.println("\nDone!");
  50.  
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement