Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼3041🔥, 0💬
Popular Posts:
JDK 11 jdk.jdeps.jmod is the JMOD file for JDK 11 JDeps tool, which can be invoked by the "jdeps" co...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
JDK 11 jdk.internal.JVM Stat.jmod is the JMOD file for JDK 11 Internal Jvmstat module. JDK 11 Intern...
JDK 11 java.sql.rowset.jmod is the JMOD file for JDK 11 SQL Rowset module. JDK 11 SQL Rowset module ...
How to perform XML Schema validation with dom\Writer.java provided in the Apache Xerces package? You...