简体   繁体   English

我正在我的 java class 中处理数据库,我需要帮助弄清楚如何更新数据库中的特定值/列

[英]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.这是我的第一个 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在第二个 class 中,我只显示了firstNamelastName ,和salary但是我还需要再次显示salary,但是这次增加了10%,或者salary += salary*.1要清楚

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.这是我的第二个 class。 How do I update the salary in here?我如何在这里更新工资? I'm assuming I need to do something with SELECT ?我假设我需要对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.对于您在 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.您可以SELECT相关更改并直接显示结果。 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() .您可以使用UPDATE的工资值创建 SQL 语句,然后使用executeUpdate()执行 SQL 语句。 This will update the Database's weeklySalary values.这将更新数据库的每周工资值。 Then, simply display the result.然后,简单地显示结果。

Under updateSalary(Connection conn) replace in accordance.根据updateSalary(Connection conn)替换。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要帮助弄清楚如何阻止我的程序抛出NullPointerException - I need help figuring out how to stop my program from throwing a NullPointerException 需要帮助弄清楚为什么我的 TestClock.java 程序出现 java.lang.StackOverflowError - Need help figuring out why I am getting a java.lang.StackOverflowError on my TestClock.java program 我需要帮助找出我做错了什么 - I need help figuring out what i did wrong 我需要帮助弄清楚为什么清除按钮代码不起作用 - I need help figuring out why my clear button code does not work 需要帮助弄清楚如何在我的程序中添加“ q”退出 - need help figuring out how to put a 'q' quit into my program 需要帮助找出我的代码中的错误 - Need help figuring it out errors with my code 需要帮助弄清楚如何大写从文本文件中读取的任何小写“i” - Need help figuring out how to capitalize any lower case "i" read from a text file 我正在尝试使用java更新数据库。 我在netbeans中工作,代码中没有错误,但行仍未更新 - I'm trying to update my database with java . i'm working in netbeans , there no error in the code but still the rows are not updated 我需要帮助将我的绘画组件添加到类的框架中 - I'm Need Help Adding My Paint Component to the Frame in a Class 我真的很难弄清楚如何使我的GUI窗口显示数组中的值 - I'm really having trouble figuring out how to make my GUI window display the values in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM