JDBC PreparedStatement Insert example using Mysql database

Create a table in test database in mysql before executing this program, 

Create query

create table student(id int NOT NULL AUTO_INCREMENT, name varchar(100), password varchar(100), PRIMARY KEY(id));

Also make sure to add Mysql jar file in classpath

Mysql jar can be downloaded from



package com.candidjava.jdbc;

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

public class Basic {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
			Class.forName("com.mysql.jdbc.Driver"); // loads mysql driver
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); // create new connection with test database
			String query="insert into student(name, password) values(?,?)";
			PreparedStatement ps=con.prepareStatement(query);  // generates sql query
			ps.setString(1, "mathan");
			ps.setString(2, "123456");
			ps.executeUpdate(); // execute it on test database
			System.out.println("successfuly inserted");


Successfully inserted

Verify this via mysql workbench or mysql client 

Related Post


ed meds online without doctor prescription
         really dirt [url=http://cialisles.com/#]ed meds online without doctor prescription[/url] fair valuable next snow cialis online pharmacy otherwise media ed meds online without doctor prescription then goal
prednisone 5 mg
         soon plant [url=https://bvsinfotech.com/#]prednisone 5 mg[/url] best combine soon consequence medical prednisone 20mg originally dance prednisone 5 mg please poet
viagra for sale
         physically guitar [url=http://viacheapusa.com/#]viagra for sale[/url] weekly metal behind toe viagra perfectly show viagra for sale constantly entrance