Java Database Connectivity (JDBC) with MySQL Server |
Hello everyone, Welcome to techxon. In this Java tutorial we discuss how to connect Java applications with MySQL Server Database. First of all need to download Connector/J Jar file for MySQL Server. You can download it from official MySQL site. After download the Jar file include it in to your project class path.
Download Connector/J Jar file for MySQL Server
Now we need to know some important information about MySQL Server.
- Connection URL - URL for connect MySQL Server
- Driver class - com.mysql.cj.jdbc.Driver
- Username - Your MySQL Server username
- Password - Your MySQL Server password
These information are mandatory. Let's discuss this tutorial step by step.
Connection URL
//Connection URL
static String url = "jdbc:mysql://localhost:3306/your_database_name";
- localhost - Server name on where MySQL is running. You can also use IP address
- 3306 - Default port number
Connection Method
I create simple method for connect MySQL Server.
//Connection Method
public Connection conn() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, "username", "password");
return connection;
}
Save, Update and Delete Method
I create method for Save data, Update data and Delete data.
//Method for Save, Update and Delete data
public void putData(String sqlQuery) throws Exception {
Statement st = conn().createStatement();
st.executeUpdate(sqlQuery);
}
Search Method
I create method for search data.
//Method for Search data
public ResultSet getData(String sqlQuery) throws Exception {
Statement st = conn().createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
return rs;
}
Complete Java Class
package yourpackagename;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author Gayan Ruchiranga
*/
public class JDBC {
//Connection URL
static String url = "jdbc:mysql://localhost:3306/your_database_name";
//Connection Method
public Connection conn() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, "username", "password");
return connection;
}
//Method for Save, Update and Delete data
public void putData(String sqlQuery) throws Exception {
Statement st = conn().createStatement();
st.executeUpdate(sqlQuery);
}
//Method for Search data
public ResultSet getData(String sqlQuery) throws Exception {
Statement st = conn().createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
return rs;
}
}
How to use this Java Class
The following example will show you how to use this JDBC Java class. First create another Java package. Enter its name as "databaseConnection". Then create a JDBC class in that package.
package yourpackagename;
import databaseConnection.JDBC;
import java.sql.ResultSet;
/**
*
* @author Gayan Ruchiranga
*/
public class TestJDBC {
public static void main(String[] args) {
try {
//Example for use putData() method
new JDBC().putData("Insert, Update or Delete query");
//Example for use getData() method
ResultSet rs = new JDBC().getData("Select query");
while (rs.next()) {
rs.getString("Column Name");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Congratulations! Now you can connect Java applications with MySQL Server Database. Remember this help and leave a comment. Thank You!
Congratulations! Now you can connect Java applications with MySQL Server Database. Remember this help and leave a comment. Thank You!
Comments
Post a Comment