MySQL JDBC Fetch Data Using Statement Interface - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

MySQL JDBC Fetch Data Using Statement Interface

Share This
You can follow the steps shown below to create a table in you database server. In our case, the database name is library and the table name is books. The SQL query to create this table is shown below.
mysql_library_book_table_struct

First, insert some data in the books table. Then, You may try the following code snippet.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.t4b.jdbc.mysql.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestMain {
static {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
String publisher = "Oxford";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library", "root", "iltwat");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from books where publisher='" + publisher + "'");
while (rs.next()) {
System.out.println(
rs.getString("name") + " : " + rs.getString("author") + " : " + rs.getString("publisher"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.