Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sampledao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * access MySQL DB by DAO pattern, print "name" of colum.
- * @author cartman
- */
- public class SampleDao {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Connection connection = null;
- PreparedStatement pre_statement = null;
- ResultSet result_set = null;
- try {
- // mySQLの接続
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?useUnicode=true&characterEncoding=utf8", "user_name", "password");
- pre_statement = connection.prepareStatement("select * from user");
- result_set = pre_statement.executeQuery();
- while (result_set.next()) {
- System.out.println(result_set.getString("name"));
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- } finally {
- closeSql(connection, pre_statement, result_set);
- }
- }
- static void closeSql(Connection con, PreparedStatement ps, ResultSet rs) {
- try {
- if (con != null) {
- con.close();
- }
- if (ps != null) {
- ps.close();
- }
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException sqle) {
- System.err.println("Error closeSql Func");
- sqle.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement