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, 2239🔥, 0💬
Popular Posts:
What Is ojdbc7.jar for Oracle 12c R1? ojdbc7.jar for Oracle 12c R1 is the JAR files of ojdbc.jar, JD...
JRE 8 deploy.jar is the JAR file for JRE 8 Java Control Panel and other deploy tools. JRE (Java Runt...
Apache Avalon began in 1999 as the Java Apache Server Framework and in late 2002 separated from the ...
What Is HttpComponents httpclient-4.2.2.jar? HttpComponents httpclient-4.2.2.jar is the JAR file for...
JSP(tm) Standard Tag Library 1.1 implementation - Jakarta Taglibs hosts the Standard Taglib 1.1, an ...