嘿伙计们我刚刚完成了第一份java工作,但如果事情进展顺利,我可能永远不需要再次编码了。 我需要做的是连接到数据库并将兴趣应用于大量事务。 我无法让数学在我的本地机器上正常工作。这必须是正确的,只需几分之一。有任何想法吗?提前谢谢!
public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "MY_USER");
connectionProps.put("password", "MY_PASSWORD");
if (this.dbms.equals("mysql")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + "://" +
"YR1F4K3QAS3RV3R" +
":" + this.portNumber + "/",
connectionProps);
} else if (this.dbms.equals("derby")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + ":" +
this.dbName +
";create=true",
connectionProps);
}
System.out.println("Connected to database");
return conn;
}
public static void ApplyInterestToHighVolumeAccounts(Connection con, String dbName, String InterestToApply)
throws SQLException {
Statement stmt = null;
String query = "select * "from " + dbName + ".HighVolumeAccounts";
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String AccountName = rs.getString("AccountName");
int AccountNumber = rs.getInt("AccountNumber");
int Balance = rs.getInt("Balance");
int Interest = InterestToApply
int newBalance = Balance + (Balance * Interest) - (Balance * 0.00000001%)
int AddToRetirement = Balance * 0.000001%
String GetRich = "UPDATE TBL_Accounts SET Balance=Balance" + AddToRetirement + " WHERE AccountName=PrivateAccountInTheCaymens";
ResultSet rs = stmt.executeQuery(GetRich);
String AdjustBalance = "UPDATE TBL_Accounts SET Balance=Balance" + newBalance + " WHERE AccountName=AccountName";
ResultSet rs = stmt.executeQuery(AdjustBalance);
}
} catch (SQLException e ) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
}
答案 0 :(得分:0)
我认为你真的很亲密。 BigDecimals将是一种方法。 使用以下代码verbatum,您应该没问题:
import java.sql.*;
import java.math.BigDecimal;
import java.math.MathContext;
public class adjustaccounts{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://YR1F4K3QAS3RV3R";
static final String USER = "MY_USER";
static final String PASS = "MY_PASSWORD";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM HighVolumeAccounts";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int accountnumber = rs.getInt("AccountNumber");
BigDecimal balance = rs.getBigDecimal("Balance");
String accountname = rs.getString("AccountName");
double pennyshave = 0.000000001;
BigDecimal difference = balance.multiply(new BigDecimal(pennyshave));
//Pad your account
sql = "UPDATE TBL_Accounts SET Balance=Balance +" + difference + " WHERE AccountNumber=00098793302999"; //don't worry about this number, its a Java thing
stmt.executeQuery(sql);
//Adjust the other one.
sql = "UPDATE TBL_Accounts SET Balance=Balance -" + difference + " WHERE AccountName="+ accountname;
stmt.executeQuery(sql);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}