Java Program-Selecting values from Database


Below is the Program which connects to database MySQL and select values from the table and displays it on the screen. Note that you must have created database and tables in MySQL Database.



import java.sql.*;
public class ConnectDatabase {

public static void main(String[] args) {

try
  {

Class.forName("com.mysql.jdbc.Driver");

try
  {

Connection            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root@123");

System.out.println("Connected");

Statement stmt=con.createStatement();

ResultSet res=stmt.executeQuery("select * from test_1");

while(res.next())

{

System.out.println(res.getInt(1)+"  "+res.getString(2));


}

}
   catch (SQLException e)
   {


e.printStackTrace();

}

}
   catch (ClassNotFoundException e)
   {

e.printStackTrace();

}

}


}



Output

Connected.

1 vishal



Explaination


  • import java.sql.*;- This statement imports sql package so that we can use it's classes and methods for interacting with Database. Java.sql package contains large number of classes and methods with the help of which we can perform various operations with Database.

  • public class ConnectDatabase- is the name of the class.

  • public static void main(String[] args)  - is the main() method . i.e entry point for every JAVA Program.

  • Class.forName("com.mysql.jdbc.Driver");-  The 'Class' is the class which is present in Java.lang package. forName is the method which returns the class for the parameter that was passed in class loader. And 'com.mysql.jdbc.Driver'  this statement is found in class path. It simply means that the driver is registered with JDBC DriverManager.

  • Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root@123");-  We have created a object of Connection class using static method getConnection(); passing the parameters the database server name jdbc:mysql hostname as localhost, database name as test , user id root and password root@123.

  • Next to execute sql query we have to create a Statement object using Statement Class. There is a createStatement() method in Connection class.

  • The ResultSet class is the class having methods to store the output of the executed query. We created object of ResultSet class passing the query using Statement object and executeQuery() method.

  • The query select * from test_1 will select all the records from the test_1 table and will be stored in ResultSet object. Then using the while loop we iterate through the records using next() method of ResultSet Class. In the loop , According to the datatype of the column of table in database, we print the value on the console. i.e res.getInt(1) prints the integer value of the first column. The (1) means the index of the column. Similarly we use the res.getString(2) to print the string value name in the second column of the table.

  • Finally the try..catch is used to find the catch any error like SQLException or ClassNotFoundException.
Java Program-Selecting values from Database Java Program-Selecting values from Database Reviewed by LanguageExpert on August 25, 2017 Rating: 5

No comments