Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)
I am working on java database connections and I do not know how to make a connection with the database and retrieve the data from it. Can anyone help me with this?

1 Answer

0 votes
by (13.1k points)

You can try something like this:

import java.sql.*;

public class EmpSal{

    public static void main(String[] args){

         try {

             

    String maximumSalary;  

   Connection connection = DriverManager.getConnection("path to database", "username", "password");

   Statement statement = connection.createStatement();

   ResultSet resultset=statement.executeQuery("select * from EmpTable order by salary desc limit 1");

     if(resultset.next()) {

           maximumSalary =  resultset.getString("firstName")+" "+resultset.getString("lastName");

      }

     System.out.println("The employee with higher salary is :");

     System.out.println( maximumSalary);

         }   

         catch (Exception exception){

     System.out.println(exception);

    }

  }

Related questions

0 votes
0 answers
asked Jun 24, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)

Browse Categories

...