[英]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 中,我只显示了
firstName
, lastName
,和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.