Query records from table (Java)
เป็นตัวอย่าง Code ในการ Query ข้อมูลมาจากฐานข้อมูลครับ โดยใช้ Statement
/** * File Name : QueryData.java * Created Date : Nov 9, 2009 : 10:43:41 AM * Copyright © 2009 www.fun4station.com */ package com.fun4station.example; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * @Author Supot Saelao * @Version 1.0 */ public class QueryData { public static void main(String[] args) { Connection conn = null; ResultSet rs = null; Statement stmt = null; try { conn = ;//getDataBase connection String sql = "SELECT ID,NAME FROM CUSTOMER"; // Create a statment object. stmt = conn.createStatement(); // Executes query to select data from table rs = stmt.executeQuery(sql); //Get all data and iterate until the last record. while (rs.next()) { //Manage data rs.getInt("ID"); //Get int column rs.getString("NAME"); //Get String column } }catch (SQLException e){ e.printStackTrace(); } finally { try{ if(rs != null) rs.close(); if(stmt != null) stmt.close(); if (conn != null) conn.close(); }catch (Exception e){} } } }
No comments yet.