classes:general:dbsamples:java_mysql
For Java database connections you will need to add the proper JDBC library to your project or classpath.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; public class JdbcMySQLVersion { public static void main(String[] args) { // Configure jdbc connection string properties as necessary for your setup. String url = "jdbc:mysql://host.cs.ndsu.nodak.edu:3306/dbname?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String user = "remoteUser"; String password = "bigsecurepassword"; String query = "SELECT table_name.column_name FROM table_name WHERE table_name.field=something"; try (Connection conn = DriverManager.getConnection(url, user, password); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query)) { while (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException ex) { Logger lgr = Logger.getLogger(JdbcMySQLVersion.class.getName()); lgr.log(Level.SEVERE, ex.getMessage(), ex); } } }
classes/general/dbsamples/java_mysql.txt · Last modified: 2020/11/02 09:03 by localadmin