0% found this document useful (0 votes)
34 views2 pages

Creating Connection To Mysql: Try Catch (Exception E)

This document provides steps to connect a Java application to a MySQL database using JDBC. It involves checking if MySQL is installed, downloading the JDBC driver, adding it to the Java classpath, importing necessary packages, creating a connection object using the DriverManager, creating a Statement object from the Connection, executing a query, and iterating through the result set to display data.

Uploaded by

Shifa Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Creating Connection To Mysql: Try Catch (Exception E)

This document provides steps to connect a Java application to a MySQL database using JDBC. It involves checking if MySQL is installed, downloading the JDBC driver, adding it to the Java classpath, importing necessary packages, creating a connection object using the DriverManager, creating a Statement object from the Connection, executing a query, and iterating through the result set to display data.

Uploaded by

Shifa Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Check if MySQL installed (direct installation | package - XAMPP/WAMP) -Default UN=root & PW=””

Download MySQL JDBC Driver

Creating Connection to MySQL

- Create a Java Project in Eclipse (Project Name: JDBC)


- Create New Class (src->r.c) (class name : testDBConnection)

- Add MySQL driver to Java classpath


----------------------------------
# Create a folder (folder name : lib) under Project Folder (R.C -> New Folder)
# Drag and drop downloaded driver into lib folder (select copy files)

# R.C on Project Folder -> Properties, Click on Java Build Path, Click on Add JARs. Select driver in lib

import java.sql.*;

Create a Try - Catch Block (optional)

try{
}
catch(Exception e){
}

Connection to MySQL DB
Connection con1 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name_optional","root","");

Creating statement
Statement st1= con1.createStatement();

Executing statement
ResultSet result = st1.executeQuery("Select * from student");
Displaying result
while(result.next()){
System.out.println(result.getString("Col1")+result.getString("Col2")+result.getSt
ring("Col3"));
}

You might also like