Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.sql.*;
  2. import com.mysql.cj.jdbc.Driver;
  3. public class SqlExample {
  4. public static void main(final String[] args) {
  5. Connection conn = null;
  6. try {
  7. DriverManager.registerDriver(new Driver());
  8. String url =
  9. "jdbc:mysql://localhost:3306/sailorboatchap5?serverTimezone=UTC&useSSL=false";
  10. // it appears you can add additional statements or whatever the semantics are after the "?" there,
  11. // such as the "useSSL=false". These appear to have to be connected with "&". The aforementioned
  12. // "useSSL=false" is to bypass a warning about permissions in accessing the database.
  13. conn = DriverManager
  14. .getConnection(url, "root", "PASSWORD");
  15. Statement stmt = conn.createStatement();
  16. String sqlthing = "SELECT * FROM faculty";
  17. //it was giving me an error earlier about "faculty" unable to be resolved as a variable, so
  18. //I just put the query in a string variable and it worked.
  19. ResultSet resultSet = stmt.executeQuery(sqlthing);
  20. while (resultSet.next()) {
  21. System.out.println(resultSet.getString("fname"));
  22. }
  23. } catch (SQLException ex) {
  24. System.out.println(ex);
  25. } finally {
  26. if (conn != null) {
  27. try {
  28. conn.close();
  29. } catch (SQLException e) { /* ignored */}
  30. }
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement