Select All Rows From Table


Leave a reply                     

Java MySql Select

How to Select MySql Table Data in Java?

Java MySql ResultSet

Java Select Query Using Statement

Query: SELECT columnname1, columnname2 FROM tablename
Above query selects all rows from table tablename contains data for columns columnname1 and columnname2

package raghuwansh.java.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SelectTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/databasename", "root", "");
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT columnname1, columnname2 FROM tablename");
			while (resultSet.next()) {
				System.out.println(resultSet.getString("columnname1") + ", "
						+ resultSet.getString("columnname2"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
/*
Output:
1, http://www.
2, raghuwansh
3, com
4, matrimony
5, blog
6, career
*/

Leave a Reply

Your email address will not be published. Required fields are marked *


one + = 3