Menampilkan Data dari Database dengan Java

       

                Langsung saja, ini adalah source code sederhana menampilkan seluruh isi tabel di dalam MySQL menggunakan JDBC.
Pastikan teman-teman telah menyertakan JAR folder ke dalam project.
Nb: project ini dibuat dengan Netbeans IDE.
Source code: ExecuteQuery2.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javamysql;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 *
 * @author Azuharu
 */
public class ExecuteQuery2 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rset = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
        }
 
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/test?"+"user=root&password=");
            stat = conn.createStatement();
            rset = stat.executeQuery("SELECT * FROM mahasiswa");
            System.out.println("no\tnama\tkelas");
            while (rset.next()) {
                System.out.println(rset.getString("id")+"\t"+rset.getString("nama")+"\t"+rset.getString("kelas"));
            }
        } catch (SQLException e) {
            System.out.println("SQLException: "+e.getMessage());
            System.out.println("SQLState: "+e.getSQLState());
            System.out.println("VendorError: "+e.getErrorCode());
        } finally{
            if(rset!=null){
                try {
                    rset.close();
                } catch (Exception e) {
                    rset = null;
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (Exception e) {
                    stat = null;
                }
            }
        }
    }
}
 
 
 
terima kasih !!!
 

0 Response to "Menampilkan Data dari Database dengan Java"

Post a Comment