2:44 am - Friday April 19, 2019

Statement EXTRACT() Example program in java jdbc mysql database

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Create Statement and insert values in the table:

mysql>create table Student(name varchar(10),course varchar(5),Attendance float(5));
Insert into Student(Arun,cse,93.45);
Insert into Student(Karthick,cse,67.78);
Insert into Student(Tamil,cse,87.89);
Query OK, 0 rows affected (0.06 sec)

Java code:

package myjava1;

import java.sql.*;

public class Extract {

	/**
	 * @param args
	 */

	// TODO Auto-generated method stub

	public static void main(String[] args) throws Exception {

		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/Arun", "root", "root");
		Statement st = conn.createStatement();

		st = conn.createStatement();
		ResultSet rs = st
				.executeQuery(" Select EXTRACT(YEAR FROM NOW()) from Student");

		while (rs.next()) {
			String name = rs.getString(1);

			System.out.println(name);

		}

		rs.close();
		st.close();
		conn.close();

	}

}

Output:


							
							
			
Filed in: Z-A All Java Codes

Comments are closed.