Retrieving Rows

Retrieving Rows

SQL Select statement:

The SELECT statement is used to select data from a table.
Syntax: Select column_names FROM table_name;
The result from a SQL query is stored in a resultset. The SELECT statement has mainly three clauses.
1). Select
2.) From
3). Where
The Select specifies the table columns that are retrieved. The From clause tells from where the tables has been accessed. The Where clause specifies which tables are used. The Where clause is optional, if not used then all the table rows will be selected.
We can see that we have used semicolon at the end of the select statement. It is used to separate each SQL statement in database systems which helps us to execute more than one SQL statement in the same call to the server.
Example
import java.io.*;
import java.sql.*;
public class InsertRecords{
public static void main(String[] args) {
System.out.println(“Insert records example using prepared statement!”);
Connection con = null;
try{
Class.forName(“com.mysql.jdbc.Driver”);
con = DriverManager.getConnection(“jdbc:mysql:
//localhost:3306/jdbctutorial”,”root”,”root”);
try{
String sql = “INSERT movies VALUES(?,?)”;
PreparedStatement prest = con.prepareStatement(sql);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter movie name:”);
String mov = bf.readLine();
prest.setString(1, mov);
System.out.println(“Enter releases year of movie:”);
int year = Integer.parseInt(bf.readLine());
prest.setInt(2, year);
int count = prest.executeUpdate();
System.out.println(count + “row(s) affected”);
con.close();
}
catch (SQLException s){
System.out.println(“SQL statement is not executed!”);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

Scroll to Top