简体   繁体   English

如何在Java中正确地从数据库检索数据

[英]how to retrieve data from database correctly in java

I wrote this code in order to display the name of employee who has the maximum salary but when the output wasn't correct it appeared null not "mmm kkk" !! 我编写此代码是为了显示具有最高薪水的员工的姓名,但是当输出不正确时,它显示为null,而不是“ mmm kkk”! although I filled the table and this is the contents : 虽然我填了桌子,这是内容:

HERE 这里

this is my code, can any one help me ?? 这是我的代码,任何人都可以帮助我吗? :( :(

 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.

Try this SQL statement: 试试这个SQL语句:

select fName, max(salary) from task4table

A problem with your code: 您的代码有问题:

In this loop you iterate to the end of the result set: 在此循环中,您迭代到结果集的末尾:

while (r1.next()) {
    size++;
}

And then, when you want to iterate again 然后,当您想再次迭代时

while (r1.next()) {
    salaries[count]= r1.getInt("salary");
    Names[count]=  r1.getString("fName")+""+r1.getString("lName");
    count++;
}

r1.next() returns false so this iteration does not happen. r1.next()返回false因此不会发生此迭代。 You either do it in one loop, or you execute query again. 您可以一次循环执行一次,也可以再次执行查询。 But, as I said, to it with proper sql statement using max . 但是,正如我所说,使用max使用适当的sql语句对其进行处理。

You change query to get all maximum salary persons in single query 您更改查询以在单个查询中获得所有最高薪水人员

PreparedStatement st = con.prepareStatement("
select fName, lName, salary from task4Table where salary = 
(select max(salary) from task4Table)");
ResultSet r1 = st.executeQuery();

Iterate this resultset. 迭代此结果集。

There's a lot wrong with your code, but here's your immediate problem: you aren't finding the max correctly. 您的代码有很多错误,但是这是您的直接问题:您找不到正确的最大值。

The right way is to use SQL, as the answer above mine shows. 正确的方法是使用SQL,正如我上面的答案所示。 If you insist on going your way, try this: 如果您坚持要走自己的路,请尝试以下操作:

int maxSalary = Integer.MIN_VALUE;
for (int i = 0; i < salaries.length; i++) {
    if (salaries[i] > maxSalary) {
        maxSalary = salaries[i];
    }
}

Your coding style is not good. 您的编码风格不好。 You need to be consistent about brace placement, spaces, blank lines, etc. You should also memorize and follow the Sun Java coding conventions. 您需要在花括号位置,空格,空白行等方面保持一致。还应该记住并遵循Sun Java编码约定。

Try following code: 尝试以下代码:

public static void displayMaxSalary() throws ClassNotFoundException, SQLException

{   

    String maxSalary;
    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 order by salary desc limit 1");
    ResultSet r1=st.executeQuery();

     if(r1.next()) {

           maxSalary =  r1.getString("fName")+""+r1.getString("lName");
      }

     System.out.println("The name of employee who has the higher salary is :");
     System.out.println( maxSalary);


} //end-displayMaxSalary.

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

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