Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
MySqlJdbcConnection.java - Connector/J JDBC Connection
How to create a JDBC connection to MySQL database using the Connector/J JDBC driver? I want to see a Java program example.
✍: FYIcenter.com
If you have a MySQL server running on your local computer
and listening on the default port 3306,
you can use the following example,
to learn how to create a JDBC connection:
// Copyright (c) FYIcenter.com import java.sql.DriverManager; import java.sql.Connection; import java.sql.DatabaseMetaData; // Example of Connector/J JDBC connection to MySQL server public class MySqlJdbcConnection { public static void main(String [] args) throws Exception { // Use the basic Connector/J JDBC connection URL String url = "jdbc:mysql://localhost?user=root&password=fyicenter"; Connection con = DriverManager.getConnection(url); System.out.println("JDBC connection and database server information:"); DatabaseMetaData meta = con.getMetaData(); System.out.println("Database Product Name: " + meta.getDatabaseProductName()); System.out.println("Database Product Version: " + meta.getDatabaseProductVersion()); System.out.println("Driver Name: " + meta.getDriverName()); System.out.println("Driver Version: " + meta.getDriverVersion()); System.out.println("JDBC Major Version: " + meta.getJDBCMajorVersion()); System.out.println("JDBC Minor Version: " + meta.getJDBCMinorVersion()); // Close the connection con.close(); } }
You can compile and run the above example in a command window as shown below:
fyicenter> javac MySqlJdbcConnection.java fyicenter> java -cp .;mysql-connector-java-5.1.40-bin.jar MySqlJdbcConnection Sat Nov 26 19:22:17 EST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. JDBC connection and database server information: Database Product Name: MySQL Database Product Version: 5.7.10-log Driver Name: MySQL Connector Java Driver Version: mysql-connector-java-5.1.40 ( Revision: ... ) JDBC Major Version: 4 JDBC Minor Version: 0
The output shows that:
You can fix the warning message by adding "useSSL=false" into the connection URL string.
⇒ MySqlJdbcUrl.java - Connector/J JDBC Connection URL
⇐ MySQL Service for Connector/J Test
2016-12-02, 2008👍, 0💬
Popular Posts:
If you are a Java developer, it is very often that you need to use some 3rd party libraries to perfo...
maven-core-3.8.6.jar is the JAR file for Apache Maven 3.8.6 Core module. Apache Maven is a software ...
Xalan-Java, Version 2.7.1, is an XSLT processor for transforming XML documents into HTML, text, or o...
Swingx is the SwingLabs Swing Component Extensions. JAR File Size and Download Location: File name: ...
What Is javamail1_1_3.zip? javamail1_1_3.zip is the binary package of JavaMail API 1.1.3 in ZIP form...