简体   繁体   中英

I'm working on databases in my java class, and I need help figuring out how to update a specific value/column in the database

Here is my first class. This is where im building my table and plugging in some values. In the second class, I am only displaying firstName , lastName , and salary BUT I also need to display the salary once more, but this time adding an increase of 10%, or salary += salary*.1 to be clear

public class CreateEmployeeDB 
{

    public static void main(String[] args) 
    {
        final String DB_URL = "jdbc:derby:EmployeeDB;create=true";
        
        try
        {
            Connection conn =
                    DriverManager.getConnection(DB_URL);
                         
            dropTables(conn);
            buildEmployees(conn);
                
            conn.close();
        }
        
        catch (Exception ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }
    

    public static void dropTables(Connection conn)
    {
        
        try
        {
            // Get a Statement object.
            Statement stmt  = conn.createStatement();
            
            try
            {
                stmt.execute("DROP TABLE Employees");
            }
            catch(SQLException ex)
            {
                // No need to report an error.
                // The table simply did not exist.
            }
        }
        catch(SQLException ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
            
    
    
    public static void buildEmployees(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            
            stmt.execute("CREATE TABLE Employees (" +
                    "socialSecurityNumber VARCHAR(30) NOT NULL PRIMARY KEY, " +
                    "firstName VARCHAR(30), " +
                    "lastName VARCHAR(30), " +
                    "weeklySalary DOUBLE, " +
                    "birthday DATE, " +
                    "employeeType VARCHAR(30), " +
                    "departmentName VARCHAR(30) " +
                    ")");
            
            
            stmt.execute("INSERT INTO EMPLOYEES VALUES( " +
                    "'11-1111', " +
                    "'John', " +
                    "'Smith', " +
                    "1000.50, " +
                    "'1945-01-02', " +
                    "'salariedEmployee', " +
                    "'R&D')");

            stmt.execute("INSERT INTO EMPLOYEES VALUES( " +
                    "'22-2222', " +
                    "'Sue', " +
                    "'Jones', " +
                    "865.00, " +
                    "'1961-02-03', " +
                    "'commissionEmployee', " +
                    "'SALES')");
            
            stmt.execute("INSERT INTO EMPLOYEES VALUES( " +
                    "'33-3333', " +
                    "'Bob', " +
                    "'Lowis', " +
                    "950.25, " +
                    "'1958-10-05', " +
                    "'basePlusEmployee', " +
                    "'SALES')");
            
            stmt.execute("INSERT INTO EMPLOYEES VALUES( " +
                    "'44-4444', " +
                    "'Karen', " +
                    "'Price', " +
                    "1100.15, " +
                    "'1972-05-25', " +
                    "'salariedEmployee', " +
                    "'HR')");
            
        }
            catch (SQLException ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
        }

    }

}

here is my second class. How do I update the salary in here? I'm assuming I need to do something with SELECT ? But I can't quite figure it out.

public class UpdateSalary
{
        public static void main(String[] args) 
        {
            final String DB_URL = "jdbc:derby:EmployeeDB";
    
            try
            {
                Connection conn = DriverManager.getConnection(DB_URL);
                
                showSalary(conn);
                
                updateSalary(conn);
                
            
            }
            catch(Exception ex)
            {
                System.out.println("ERROR: " + ex.getMessage());
            }
    
        }
        
        public static void showSalary(Connection conn)
        {
            final String DB_URL = "jdbc:derby:Personnel";
            
            try
            {
    
                Statement stmt = conn.createStatement();
                
                String sqlStatement = "SELECT * FROM Employees";
    
                ResultSet result = stmt.executeQuery(sqlStatement);
                
                System.out.println("\n\t\t\t\t Employees Report - Current Salary ");
                System.out.println("-------------------------------");
                System.out.println("\t\t\t   First \t\t\tLast  Salary");
    
                while (result.next())
                {
                    System.out.printf("%30s %30s $%5.2f\n",
                            
                            result.getString("firstName"),
                            result.getString("lastName"),
                            result.getDouble("weeklySalary")
                            );
                }
                
                //conn.close();
            
            }
                catch(Exception ex)
                {
                    System.out.println("ERROR: " + ex.getMessage());
                }
        }
        
        public static void updateSalary(Connection conn)
        {
            final String DB_URL = "jdbc:derby:Personnel";
            
            try
            {
    
                Statement stmt = conn.createStatement();
                
                String sqlStatement = "SELECT weeklySalary FROM Employees  ";
    
                ResultSet result = stmt.executeQuery(sqlStatement);
                
                System.out.println("\n\t\t\t\t Employees Report - Updated Salary ");
                System.out.println("----------------------------------");
                System.out.println("\t\t\t   First \t\t\tLast  Salary");
    
                while (result.next())
                {
                    System.out.printf("%30s %30s $%5.2f\n",
                            
                            result.getString("firstName"),
                            result.getString("lastName"),
                            result.getDouble("weeklySalary")
                            );
                }
                
                conn.close();
            
            }
                catch(Exception ex)
                {
                    System.out.println("ERROR: " + ex.getMessage());
                }
        }
    
    }

i apologize for the messy code and weird formatting that you get in the output. If anyone has tips, I would appreciate it. Thank you for your time.

You can SELECT it by making the relevant changes and display the result directly. Although this does not affect the database's weeklySalary values.

SELECT firstName,lastName,weeklySalary*1.1 as weeklySalary FROM Employees 

You can create a SQL statement using UPDATE with the updated salary value and then execute the SQL statement using executeUpdate() . This will update the Database's weeklySalary values. Then, simply display the result.

Under updateSalary(Connection conn) replace in accordance.

        String updateSqlStatement = "UPDATE Employees SET weeklySalary = weeklySalary*1.1";
        stmt.executeUpdate(updateSqlStatement);
        String sqlStatement = "SELECT * FROM Employees";
        ResultSet result = stmt.executeQuery(sqlStatement);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM