Database Connection Query Code
Connection Query
try{}
The try statement lets you test a block of code for errors.catch(Exception e)
The catch statement lets you handle the error, and Exception is the Handle these error amd store the object name like e.
Show Data
Statement – Used to execute string-based SQL queries, and we can't pass the Parameter in Statement.
PreparedStatement – Used to execute parameterized SQL queries, like Insert or Update
ResultSet – The term "result set" refers to the row and column data contained in a ResultSet object.It is used to store the data which are returned from the database table after the execution of the SQL statements in the Java Program.
Commonly used methods of ResultSet interface:
public boolean next():
is used to move the cursor to the one row next from the current position, and we use the next() method in the while loop.if the cursor move to next rows if the rows find then it is true looop execute and then forward, At the End the rows not fatch then while condition false.In Blow Example you can See the next() method use.
public boolean previous():
is used to move the cursor to the one row previous from the current position.
e.g If we have 8 rows in the table then we use the first rs.afterLast(); By using the afterLast() method cursor move the last position and then you write the rs.previous(); and you see the first position last record print that means that the cursor move backword 8 rows to 1st rows.public boolean first():
is used to move the cursor to the first row in result set object.
public boolean last():
is used to move the cursor to the last row in result set object.
public boolean absolute(int row):
is used to move the cursor to the specified row number in the ResultSet object.If you pass 3 value in rs.absolute(3); then only 3 value fatch.
public boolean relative(int row):
is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.
public int getInt(int columnIndex):
is used to return the data of specified column index of the current row as int.
public int getInt(String columnName):
is used to return the data of specified column index of the current row as int.
public String getString(int columnIndex):
is used to return the data of specified column index of the current row as int.
public String getString(String columnName):
is used to return the data of specified column index of the current row as int.
Update Data
PreparedStatement pst = con.prepareStatement(query_name); is used to execute the query or update the Query instead using the Statement. PreparedStatement in Java allows you to write a parameterized query which gives better performance than Statement class in Java. PreparedStatement is very easy to use Parametrized constructor like pst.setString(1,variable_name) or pst.setInt(2,variable_name); and at last you can Enter the pst.executeUpdate();
The Java JDBC PreparedStatement primary features are:
- Easy to insert parameters into the SQL statement.
- Easy to reuse the PreparedStatement with new parameter values.
- May increase performance of executed statements.
- Enables easier batch updates
pst.executeUpdate(); – It will return 1, means that one rows Effected in the Database and the Data in the touple are added Sucessfully.If it will return 0 then means that the errror in the Data you Enter.int Add_Touble = pst.executeUpdate(); we print the Add Touple value the show the 1 or 0. 1 mean added data and 0 mean data not added.
Insert Data
Delete Data
Search Record
Download Java JDBC Complete Crud Operation code
Java JDBC Simple LMS Code
Use of String.Format();
string.format() is used to the better performance in the output of the String.By the use of string.format() the output string should be made Stylish.We have Pass the arguments in this format() method.
There are many format specifiers we can use. Here are some common ones:
- %a- floating point (except BigDecimal)
- %b- Any type
- %c- Character
- %d- integer (incl. byte, short, int, long, bigint)
- %e - Exponential floating-point number
- %f - Floating-point number
- %g - floating point
- %h- any type
- %i - Integer (base 10)
- %o - Octal number (base 8)
- %s - String
- %u - Unsigned decimal (integer) number
- %x - Hexadecimal number (base 16)
- %t - Date/time
- %n - Newline
In Blow Example of JDBC Course Section.I have Design in the format by using string.format().
Post a Comment