Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testmysql;
- import java.sql.*;
- public class JavaMysqlSelectExample {
- public static void main(String[] args) {
- try {
- // create our mysql database connection
- String myDriver = "org.gjt.mm.mysql.Driver";
- String myUrl = "jdbc:mysql://localhost/TestMySQL";
- Class.forName(myDriver);
- Connection conn = DriverManager.getConnection(myUrl, "root", "admin");
- // our SQL SELECT query.
- // if you only need a few columns, specify them by name instead of using "*"
- String query = "SELECT * FROM user";
- // create the java statement
- Statement st = conn.createStatement();
- // execute the query, and get a java resultset
- ResultSet rs = st.executeQuery(query);
- // iterate through the java resultset
- while (rs.next()) {
- int id = rs.getInt("id");
- String username = rs.getString("username");
- String password = rs.getString("password");
- int level = rs.getInt("level");
- // print the results
- System.out.format("%s, %s, %s, %s\n", id, username, password, level);
- }
- st.close();
- } catch (Exception e) {
- System.err.println("Got an exception! ");
- System.err.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment