Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.sql.*;
- public class PrintdepartmentEmps {
- private static final String dbServer ="LAPTOP-3U8MO386\\SQLEXPRESS;"; //Insert your own server name here!
- private static final String dbDb = "Company;";
- private static String dbLoginUser = "sa;"; // Careful! Plain text username/PW
- private static String dbPassword = "system"; // Careful! Plain text username/PW
- private static boolean isWindowsAuthentication = true; // Choose Windows/SQL Server Authentication here
- public static void main(String[] args) throws SQLException, InterruptedException {
- String connectionString;
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- } catch (ClassNotFoundException driverNotFound) {
- System.out.println("Driver could not be loaded");
- }
- if (isWindowsAuthentication) {
- connectionString = "jdbc:sqlserver://" + dbServer + "databaseName=" + dbDb + "integratedSecurity=true";
- }
- else {
- connectionString = "jdbc:sqlserver://" + dbServer + "databaseName=" + dbDb + "user=" + dbLoginUser + "password=" + dbPassword;
- }
- Connection conn = DriverManager.getConnection(connectionString); // For now, the most interesting object
- //
- String lName;
- double salary;
- System.out.print("Hello DB World!\n");
- System.out.print(connectionString + "\n");
- System.out.print("Enter a Department number: ");
- Scanner sc = new Scanner(System.in);
- Integer dno = sc.nextInt();
- sc.close();
- String query = "select lname, salary from EMPLOYEE where dno = " + dno.toString();
- Statement statem = conn.createStatement();
- ResultSet rs = statem.executeQuery(query);
- System.out.println("\nData:");
- while (rs.next()) {
- lName = rs.getString(1);
- salary = rs.getDouble(2);
- System.out.println(lName + " " + (int)salary);
- }
- System.out.println("\nDone!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement