Posted in MySQL 12 years ago 2 min read
/*
* 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;
}
}
}
}
}
Semoga bermanfaat.