Java With MySQL Insert Example

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 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 with Mysql Insert Full Program:

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!");
	}

}

Output:

Insert started!
Insertion Done!

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:

720 total views, 1 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in