Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import com.mysql.cj.jdbc.Driver;
- public class SqlExample {
- public static void main(final String[] args) {
- Connection conn = null;
- try {
- DriverManager.registerDriver(new Driver());
- String url =
- "jdbc:mysql://localhost:3306/sailorboatchap5?serverTimezone=UTC&useSSL=false";
- // it appears you can add additional statements or whatever the semantics are after the "?" there,
- // such as the "useSSL=false". These appear to have to be connected with "&". The aforementioned
- // "useSSL=false" is to bypass a warning about permissions in accessing the database.
- conn = DriverManager
- .getConnection(url, "root", "PASSWORD");
- Statement stmt = conn.createStatement();
- String sqlthing = "SELECT * FROM faculty";
- //it was giving me an error earlier about "faculty" unable to be resolved as a variable, so
- //I just put the query in a string variable and it worked.
- ResultSet resultSet = stmt.executeQuery(sqlthing);
- while (resultSet.next()) {
- System.out.println(resultSet.getString("fname"));
- }
- } catch (SQLException ex) {
- System.out.println(ex);
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) { /* ignored */}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement