Skip to main content

MYSQL JDBC Connection in Java

Sample code to demonstrate MYSQL JDBC connectivity in Java

Download mysql-connector-java-5.0.7.jar and add to your build path to run the code.

Setup the project as show in the image below.

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.ResultSet;

public class TestConnection {

public static void main(String[] argv) {

System.out.println("******** MySQL JDBC Connection ********");
ResultSet rs = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL JDBC Driver Loaded");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement stmt = connection.createStatement();
rs= (ResultSet) stmt.executeQuery("select * from student");
while (rs.next()) {
System.out.println(rs.getInt(1) + "==" +rs.getString(2) + "==" +rs.getString(3));
} //end while
connection.close();
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not loaded");
e.printStackTrace();
return;
}
catch (SQLException e)
{
System.out.println("Connection Failed!");
e.printStackTrace();
return;
}
  }
}


Output as shown in below Image

Comments

Popular posts from this blog

Download Report in Excel in JSP

Report in Excel Format in JSP FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED AT 11:43 BA 4081 PARIS-CDG 11:45 11:57 LANDED AT 11:58 BE 843 BELFAST CITY 11:45 11:40 LANDED AT 11:41 Call the above jsp by using any of the click functionality from another .jsp

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...