Java With MySQL Select Example

java-with-mysql-example-featured
java-with-mysql-example-blog

Download the users_test table:
Users_test table – Mysql download

Loading the Mysql connection:

public Connection LoadDriver() {
Connection connection = null;
try {
// Driver Name
String mySQLDriver = "org.gjt.mm.mysql.Driver";
// Path/database name [here guru is the database name]
String url = "jdbc:mysql://localhost:3306/guru";
Class.forName(mySQLDriver);
// User name and password
connection = DriverManager.getConnection(url, "root", "pass123");

} catch (Exception exception) {
System.err.println("Exception! ");
System.err.println(exception.getMessage());
}
return connection;
}

Fetching the records: [Select]

public void getRecords(Connection connection) {
String query = "SELECT * FROM users_test";
Statement createStatement;
try {
createStatement = connection.createStatement();
ResultSet resultSet = createStatement.executeQuery(query);
while (resultSet.next()) {
String mobile = resultSet.getString("mobile");
String full_name = resultSet.getString("full_name");
String email = resultSet.getString("email");
System.out.format("%s, %s, %s\n", full_name, email, mobile);
}
createStatement.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}

}

Mysql With Select Full Program:

package in.javadomain;

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

/**
* @author Naveen
* class to connect java with mysql and fetching mysql records
* using java
*/
public class JavaMySQL {
/**
* Main method first get the mysql connection then it will fetch the records
*/
public static void main(String[] args) {
JavaMySQL javaMySQL = new JavaMySQL();
// Getting the mysql Connection
Connection conn = javaMySQL.LoadDriver();
// Fetching the mysql table records
javaMySQL.getRecords(conn);
}

/**
* Loads the mysql connection with the given username, password, database
* and mysql jar driver
*
* @return Connection
*/
public Connection LoadDriver() {
Connection connection = null;
try {
// Driver Name
String mySQLDriver = "org.gjt.mm.mysql.Driver";
// Path/database name [here guru is the database name]
String url = "jdbc:mysql://localhost:3306/guru";
Class.forName(mySQLDriver);
// User name and password
connection = DriverManager.getConnection(url, "root", "pass123");

} catch (Exception exception) {
System.err.println("Exception! ");
System.err.println(exception.getMessage());
}
return connection;
}

/**
* @param connection
* to create the mysql connection Records fetched using create
* statement
*/
public void getRecords(Connection connection) {
String query = "SELECT * FROM users_test";
Statement createStatement;
try {
createStatement = connection.createStatement();
ResultSet resultSet = createStatement.executeQuery(query);
while (resultSet.next()) {
String mobile = resultSet.getString("mobile");
String full_name = resultSet.getString("full_name");
String email = resultSet.getString("email");
System.out.format("%s, %s, %s\n", full_name, email, mobile);
}
createStatement.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}

}
}

Output:

Hems, Hems@Hems.com, 7777777777
Aravind, aravind@aravind.com, 7777788888
Chudar, chudar@chudar.com, 8888888888
Naveen, naveen@naveen.com, 9999999999

Download the Source code:
Java with Mysql Select

Note:
1. Do not forget to add the mysql java connection jar.
2. Mention the username, password and database name correctly.
3. Mention the column names, column name types and table names correctly.

Leave a Reply