Friday, March 14, 2014
In our last post we discussed about how to obtain a database connection.After we have it, we can carry out different JDBC operations. Here we will discuss about
- How to insert new records/rows in database tables (INSERT operation)
- How to find a record/row in database tables (SELECT operation)
- How to update records/rows in tables (UPDATE operation)
- How to delete a record/row from tables (DELETE operation)
Different JDBC operations |
SQL is written for carrying out the operations and it is either passed to a Statement or a PreparedStatement. The latter is used as DBMS has to only recompile and carry out the task. There is no need of parsing if PreparedStatement is used. Also it prevents SQL injection. You should use it everywhere when you have to pass parameters. It is recommended to use in all public applications.
First create a database table Student as follows :
CREATE TABLE student(id number primary key,name varchar(30));
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StudentService { private Connection con; public StudentService(Connection con){ this.con = con; //database connection object } //Inserting new row in table with given id and name public void insertRecord(int id, String name) throws SQLException{ con.setAutoCommit(false); String sql = "INSERT INTO student VALUES(?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ps.setString(2, name); ps.executeUpdate(); con.commit(); ps.close(); } //Find a record based on id public void findRecord(int id) throws SQLException{ String sql = "SELECT * FROM student WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if(rs.next()) System.out.println(rs.getInt("id")+"\t"+rs.getString("name")); else System.out.println("No records found"); ps.close(); } //Find all the records public void findAllRecord() throws SQLException{ con.setAutoCommit(false); String sql = "SELECT * FROM student"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()) System.out.println(rs.getInt("id")+"\t"+rs.getString("name")); ps.close(); } //Update a record with new name based on id public void updateRecord(int id, String name) throws SQLException{ con.setAutoCommit(false); String sql = "SELECT id,name FROM student WHERE id=?"; PreparedStatement ps=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ps.setInt(1, id); ResultSet rs=ps.executeQuery(); rs.updateString("name", name); rs.updateRow(); con.commit(); ps.close(); } //Delete a record based on id public void deleteRecord(int id) throws SQLException{ con.setAutoCommit(false); String sql = "DELETE FROM student WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); con.commit(); ps.close(); } }
-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------
Labels:JDBC
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment