Guest User

Untitled

a guest
Jan 15th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class DBConnect {
  4. public static void main(String[] args) throws Exception
  5. {
  6. // load driver, for JDBC 3 and earlier
  7. Class.forName("com.mysql.jdbc.Driver");
  8. // make the connection using the supplied credentials
  9. Connection connect =
  10. DriverManager.getConnection("jdbc:mysql://localhost:8889/java_demo","root","root");
  11. // execute a statement and get the resultSet
  12. Statement st = connect.createStatement();
  13. ResultSet resultSet = st.executeQuery("SELECT * FROM messages");
  14. // iterate through the resultSet and print them out
  15. while (resultSet.next())
  16. {
  17. System.out.println(resultSet.getInt("id") + " "
  18. + resultSet.getString("user") + " : "
  19. + resultSet.getString("message"));
  20. }
  21. // clean up for earlier JVM's
  22. connect.close();
  23. st.close();
  24. resultSet.close();
  25. }
  26. }
Add Comment
Please, Sign In to add comment