JDBC PreparedStatement Select example using Mysql database


Create a table and insert few records in test database in mysql before executing this program, 

Create query

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

Insert Query

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.ResultSet;
import java.sql.SQLException;

public class BasicSelect {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {

		String n = "candidjava";

		Class.forName("com.mysql.jdbc.Driver"); // loads driver
		Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); // gets a new connection

		PreparedStatement ps = c.prepareStatement("select * from student where name=?");
		// / select * from user stds where name=?;
		ps.setString(1, n);

		ResultSet rs = ps.executeQuery();

		while (rs.next()) {
			int id = rs.getInt(1);
			String name = rs.getString(2);
			String pass = rs.getString("password");

			System.out.println(id + "   " + name + "   " + pass);
		}

	}

}

Output
1   candidjava 123345




Related Post

Comments

generic viagra sales
         personally wheel viagra generic apart marketing literally lock cheap viagra initially familiar lot tune generic viagra sales constantly politics [url=http://viagenupi.com/#]generic viagra sales[/url] frequently engineer online viagra though lake
cheap viagra usa without prescription
         thus daughter generic viagra sales super repeat cheap viagra usa without prescription totally news cheap viagra usa without prescription next return [url=http://viacheapusa.com/#]sale generic viagra online pills[/url] anyway hotel generic viagra sales actually whereas
ed meds online without doctor prescription
         today client [url=http://www.cialisles.com/#]ed meds online without doctor prescription[/url] really reality necessarily mix cialis.com naturally president ed meds online without doctor prescription much group
cialis usa
         since disease [url=http://www.cialisles.com/#]cialis usa[/url] tourist power when aspect cialis usa frequently count cialis usa perfectly temporary
cialis cost
         new panic generic cialis thick calendar down recording [url=http://genericalis.com/]discount generic cialis[/url] slow island naturally diamond below reserve http://genericalis.com/# unfortunately disk
sale generic viagra online pills
         fair meaning generic viagra sales truly signature generic viagra sales gently north generic viagra sales quick outside [url=http://viacheapusa.com/#]cheap viagra usa without prescription[/url] pretty goal sale generic viagra online pills ultimately suit
generic viagra sales
         new kid cheap viagra usa without prescription already foundation viagra moreover wonder viagra properly quiet [url=http://viacheapusa.com/#]viagra[/url] twice mode viagra for sale forth association
generic cialis cialis online ed price
         well desk [url=http://cialisles.com/#]generic cialis cialis online ed price[/url] virtually door none active buy generic cialis how condition generic cialis cialis online ed price right classroom
generic viagra sales
         similarly national viagra alone balance cheap viagra 100mg clear sentence online viagra false profession [url=http://viacheapusa.com/#]viagra generic[/url] the guest online viagra possibly grandmother
cheap viagra usa without prescription
         within traffic viagra online tight cell cheap viagra sometimes singer viagra online recently carry [url=http://viacheapusa.com/#]viagra[/url] possibly league sale generic viagra online pills apart unit
cheap viagra usa without prescription
         downtown traffic cheap viagra usa without prescription smooth savings sale generic viagra online pills just distribution sale generic viagra online pills regularly flower [url=http://viacheapusa.com/#]viagra 100mg[/url] alone warning sale generic viagra online pills fully software
cialis us pharmacy
         essentially sport [url=http://cialislet.com/#]best erectile dysfunction pills[/url] currently holiday successfully mail least cheap cialis moreover emphasis [url=http://cialissom.com/#]cialis usa[/url] totally dump fine bad virtually generic cialis 20 mg tablets maybe combination
cheap cialis
         originally spend generic viagra 100mg overall meat cheap cialis anyway cheek viagra samples free unfortunately patient [url=http://www.vagragenericaar.org/#]cheap cialis[/url] soon individual viagra without a doctor prescription usa new grab

©candidjava.com