PostgreSql as discussed in earlier posts is an advanced database.Now in this post I am going to discuss the two basic database operations in Postgresql i.e. Insert,Select.The remaining two operations i.e. Update,Delete will be discussed in the next post.
1. Insert operation in Postgresql database
As the name suggests,this operation is basically used for Inserting values into your database,below is the command used to Insert data into database using Insert statement.
2.Select Operation in Postgresql database:
Since the insert operation helps us to insert the records into database,and the select statement help us to select the respective records specified in the query.
You can simply select a record using Select statement just like below:
Here is the complete code snippet for illustrating the use of Select operation
1. Insert operation in Postgresql database
As the name suggests,this operation is basically used for Inserting values into your database,below is the command used to Insert data into database using Insert statement.
"INSERT INTO TABLE_NAME(COLUMN1,COLUMN2,COLUMN3)"+ "VALUES(1,'TROY',35);";Here is the complete code snippet for the Insert operation:
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Insert_operation {
public static void main(String args[])
{
try
{
Statement st = null;
Connection c = null;
Class.forName("org.postgresql.Driver");
c=DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar","postgres", "osm");
c.setAutoCommit(false);This way your records are successfully saved into the database.
st = c.createStatement();
String sql;
sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+"VALUES(1,'Tarsem',35);";
st.executeUpdate(sql);
sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(2,'Gurpreet',36);";
st.executeUpdate(sql);
sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(3,'Jaspreet',37);";
st.executeUpdate(sql);
sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(4,'Maninder',39);";
st.executeUpdate(sql);
sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(5,'Ranjeet',38);";
st.executeUpdate(sql);
st.close();
c.commit();
c.close();
}
catch(Exception e)
{
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Records created successfully");
}
}
2.Select Operation in Postgresql database:
Since the insert operation helps us to insert the records into database,and the select statement help us to select the respective records specified in the query.
You can simply select a record using Select statement just like below:
"SELECT * FROM TABLE_NAME;"
Here is the complete code snippet for illustrating the use of Select operation
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Select_operation {
public static void main(String args[])
{
Connection c = null;
Statement st = null;
try
{
Class.forName("org.postgresql.Driver");
c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar" ,"postgres", "osm");
c.setAutoCommit(false);
st = c.createStatement();
ResultSet rs = st.executeQuery( "SELECT * FROM NEW_GARAGE;" );
while ( rs.next() )
{
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "AGE = " + age );
System.out.println();
}
rs.close();
st.close();
c.close();
}
catch(Exception e)
{
System.err.println(e.getClass().getName()+": "+ e.getMessage());
System.exit(0);
}
System.out.println("Operation performed successfully");
}
}
Here is the output of the Select query:
Happy learning.Stay tuned.
0 comments: