package com.t4b.jdbc.mysql.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestMain {
static {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library", "root", "pass123");
pstmt = con.prepareStatement("select * from books");
rs = pstmt.executeQuery();
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 {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
MySQL JDBC Fetch Data Using PreparedStatement Interface
Share This
Tags
# Java Database Connection
# Technology
Share This
About BunksAllowed
Technology
Labels:
Java Database Connection,
Technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.