Another, similar, JDBC example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo {
public static void main (String[] args) {
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=root&password=csc314");
// Do something with the Connection
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM student");
System.out.println("ID: " + "\t" + "\t" + "Name: ");
while (res.next()) {
int i = res.getInt("idStudent");
String s = res.getString("Name");
System.out.println(i + "\t\t" + s);
}//while
//System.out.println("Connection worked");
}//try
catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
This is what is produced (Moodle wrecks the tabs):
ID: Name:
1 Alex
2 Wint