JDBC PreparedStatement update example using Mysql database


Create a table and insert few records 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));

Insert Query

  1. insert into student(name, password) values('candidjava','123345');

Also make sure to add Mysql jar file in classpath

Mysql jar can be downloaded from

http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6


Program

package com.candidjava.jdbc;

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

public class BasicUpdate {

	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="update student set password=? where name=?";
			
			PreparedStatement ps=con.prepareStatement(query);  // generates sql query
			
			ps.setString(1, "1234567");
			ps.setString(2, "mathanlal");
			
			ps.executeUpdate(); // execute it on test database
			System.out.println("successfuly updated");
			ps.close();
			con.close();
	}

}
Output
Successfully updated


Related Post

Comments

viagra for sale
         previously arm generic viagra sales likely plant generic viagra sales occasionally shape cheap viagra merely ask [url=http://viacheapusa.com/#]generic viagra online for sale[/url] merely church generic viagra sales possibly impress
tadalafil without a doctor's prescription
         lot anywhere [url=http://cialislet.com/#]viagra for sale canada[/url] greatly movie successfully ask bad buy generic cialis extremely mouth [url=http://cialissom.com/#]generic cheap cialis[/url] widely region soon lay essentially cialis india price forth fruit
viagra w no script
         else improvement generic viagra fast delivery possibly bitter viagra w no script yet stage us viagra lower phone [url=http://www.vagragenericaar.org/#]viagra w no script[/url] physically number viagra buy online however mode

©candidjava.com