我编写此代码是为了显示具有最高薪水的员工的姓名,但是当输出不正确时,它显示为 null 而不是“mmm kkk”!虽然我填满了表格,但这是内容:
这是我的代码,任何人都可以帮助我吗?:(
public static void displayMaxSalary() throws ClassNotFoundException, SQLException
{
int size=0;
int count=0;
String maxSalary=null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/task4DB?"
+ "user=root&password=123");
PreparedStatement st = con.prepareStatement("select * from task4Table ");
ResultSet r1=st.executeQuery();
while (r1.next()) {
size++;
}
int salaries[]=new int[size];
String Names[]=new String[size];
while (r1.next()) {
salaries[count]= r1.getInt("salary");
Names[count]= r1.getString("fName")+""+r1.getString("lName");
count++;
}
for(int i=1;i< salaries.length;i++)
{
if(salaries[i]>salaries[i-1])
{
maxSalary= Names[i];
}
}
System.out.println("The name of employee who has the higher salary is :");
System.out.println( maxSalary);
} //end-displayMaxSalary.