Retrieving All Rows from a Database Table
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] argv) throws Exception {
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM emp");
while (res.next()) {
int i = res.getInt("ID");
String s = res.getString("name");
System.out.println(i + "\t\t" + s);
}
con.close();
}
}
Related examples in the same category