Java With MySQL Insert Example

Download the users_test table:
Users_test table – Mysql download

Loading the Mysql connection:

[java]
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;
}
[/java]

Fetching the records: [Select]

[java]
public void insertUsers(Connection conn, String email,String full_name,String mobile,String pass,String access){
System.out.println("Insert started!");
String query = " insert into users_test (mobile,email,pass,full_name, access) values (?, ?, ?, ?,?)";
PreparedStatement preparedStmt;
try {
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, mobile);
preparedStmt.setString(2, email);
preparedStmt.setString(3, pass);
preparedStmt.setString(4, full_name);
preparedStmt.setString(5, access);
preparedStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}

System.out.println("Insertion Done!");
}
[/java]

Java with Mysql Insert Full Program:
[java]
package in.javadomain;

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

/**
* @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) {
String email = "me@ngdeveloper.com";
String full_name = "naveenkumar";
String mobile = "9597470873";
String pass = "testpass";
String access = "1";
JavaMySQL javaMySQL = new JavaMySQL();
// Getting the mysql Connection
Connection conn = javaMySQL.LoadDriver();
// Fetching the mysql table records
javaMySQL.insertUsers(conn, email, full_name, mobile, pass, access);
}

/**
* 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 conn
* @param email
* @param full_name
* @param mobile
* @param pass
* @param access
* all the above params are inserted using the prepared statement
*/
public void insertUsers(Connection conn, String email, String full_name,
String mobile, String pass, String access) {
System.out.println("Insert started!");
String query = " insert into users_test (mobile,email,pass,full_name, access) values (?, ?, ?, ?,?)";
PreparedStatement preparedStmt;
try {
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, mobile);
preparedStmt.setString(2, email);
preparedStmt.setString(3, pass);
preparedStmt.setString(4, full_name);
preparedStmt.setString(5, access);
preparedStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}

System.out.println("Insertion Done!");
}

}

[/java]

Output:
[plain]
Insert started!
Insertion Done!
[/plain]

Download the Source code:
Java With Mysql Insert

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.

Java Recommended Books:

Leave a Reply