什么是MySQL JDBC驱动程序连接字符串?
我是JDBC新手,我正在尝试连接到MySQL数据库。什么是MySQL JDBC驱动程序连接字符串?,mysql,jdbc,connection-string,Mysql,Jdbc,Connection String,我是JDBC新手,我正在尝试连接到MySQL数据库。 我使用的是Connector/J驱动程序,但我找不到我的类.forName()方法的JDBC连接字符串。假设驱动程序在路径中 String url = "jdbc:mysql://localhost/test"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); Connection conn = DriverManager.getConnection (url, "usernam
我使用的是Connector/J驱动程序,但我找不到我的
类.forName()
方法的JDBC连接字符串。假设驱动程序在路径中
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection (url, "username", "password");
以下是文档: 基本连接字符串如下所示:
jdbc:mysql://localhost:3306/dbname
class.forName字符串是“com.mysql.jdbc.Driver”,您可以找到它(编辑:现在在同一页上)
从oracle文档中
jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]
host:port是承载数据库的计算机的主机名和端口号。如果未指定,主机和端口的默认值分别为127.0.0.1和3306
数据库是要连接到的数据库的名称。如果未指定,则在没有默认数据库的情况下进行连接
故障转移是备用数据库的名称(MySQL Connector/J支持故障转移)
propertyName=propertyValue表示可选的、以符号和分隔的属性列表。这些属性使您能够指示MySQL Connector/J执行各种任务。非常简单:
和
在连接选项卡中给出。它主要是localhost:3306<代码>将在Windows服务名称的“系统配置文件”选项卡下找到。默认值主要是MySQL5
,其中x是版本号,例如MySQL5.6为56,MySQL5.5为55等。您也可以指定自己的Windows服务名称进行连接3306
是mysql的默认端口
如果您使用的是Java7,那么甚至不需要添加Class.forName(“com.mysql.jdbc.Driver”).newInstance()代码>语句。自动资源管理(ARM)是在JDBC4.1中添加的,默认情况下在Java7中提供
用于连接MySQL服务器的JDBC URL的一般格式如下,方括号([])中的项目是可选的:
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] »
[?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
对于Mysql,jdbc驱动程序连接字符串为com.Mysql.jdbc.Driver。使用以下代码进行连接:-
class DBConnection {
private static Connection con = null;
private static String USERNAME = "your_mysql_username";
private static String PASSWORD = "your_mysql_password";
private static String DRIVER = "com.mysql.jdbc.Driver";
private static String URL = "jdbc:mysql://localhost:3306/database_name";
public static Connection getDatabaseConnection(){
Class.forName(DRIVER);
return con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
}
协议//[主机][数据库][属性]
如果你没有任何属性忽略它,那么它会像
jdbc:mysql://127.0.0.1:3306/test
jdbc:mysql是协议
127.0.0.1:是主机,3306是端口号
测试是数据库因为答案似乎已经得到了回答,没有太多需要补充的地方,但我想在现有答案的基础上补充一点。
这是为mysql的JDBC驱动程序加载类的方法
com.mysql.jdbc.Driver
但现在这种方法已被弃用。新的驱动程序类现在是
com.mysql.cj.jdbc.Driver
此外,驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。mySQL 8更新:
String jdbcUrl=“jdbc:mysql://localhost:3306/youdatabase?useSSL=false&serverTimezone=UTC";代码>这取决于您使用的服务
如果您使用MySQL Workbench,它将是这样的:
jdbc:mysql://“主机”:“端口号”/
当然,如果使用SSL/SSH,情况会有所不同
有关更多信息,请访问Jetbriens(intelliJ idea)的官方链接:
mySQL 8的更新:
String jdbcUrl=“jdbc:mysql://localhost:3306/youdatabase?useSSL=false&serverTimezone=UTC";强>
使用以下代码段检查Jdbc配置和URL是否正确
import java.sql.Connection;
import java.sql.DriverManager;
public class TestJdbc {
public static void main(String[] args) {
//db name:testdb_version001
//useSSL=false (get rid of MySQL SSL warnings)
String jdbcUrl = "jdbc:mysql://localhost:3306/testdb_version001?useSSL=false";
String username="testdb";
String password ="testdb";
try{
System.out.println("Connecting to database :" +jdbcUrl);
Connection myConn =
DriverManager.getConnection(jdbcUrl,username,password);
System.out.println("Connection Successful...!");
}catch (Exception e){
e.printStackTrace();
//e.printStackTrace();
}
}
}
方法Class.forName()
用于注册JDBC驱动程序。连接字符串用于检索到数据库的连接
检索数据库连接的方法如下所示。理想情况下,由于您不希望创建到数据库的多个连接,请将连接限制为一个,然后重新使用同一个连接。因此,在处理到数据库的连接时,在这里使用单例模式
下图显示了检索连接的连接字符串:
public class Database {
private String URL = "jdbc:mysql://localhost:3306/your_db_name"; //database url
private String username = ""; //database username
private String password = ""; //database password
private static Database theDatabase = new Database();
private Connection theConnection;
private Database(){
try{
Class.forName("com.mysql.jdbc.Driver"); //setting classname of JDBC Driver
this.theConnection = DriverManager.getConnection(URL, username, password);
} catch(Exception ex){
System.out.println("Error Connecting to Database: "+ex);
}
}
public static Database getDatabaseInstance(){
return theDatabase;
}
public Connection getTheConnectionObject(){
return theConnection;
}
}
检查驱动程序连接器jar是否与SQL版本匹配
当我使用
mySQl-connector-java-5.1.30.jar
对于MySql 8,这里是我这边的一段小代码:)
所需驱动程序:
com.mysql.jdbc.Driver
下载:(与平台无关)
一行中的连接字符串:
jdbc:mysql://localhost:3306/db-name?user=user_name&password=db_password&useSSL=false
示例代码:
public static void testDB(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db-name?user=user_name&password=db_password&useSSL=false");
if (connection != null) {
Statement statement = connection.createStatement();
if (statement != null) {
ResultSet resultSet = statement.executeQuery("select * from test");
if (resultSet != null) {
ResultSetMetaData meta = resultSet.getMetaData();
int length = meta.getColumnCount();
while(resultSet.next())
{
for(int i = 1; i <= length; i++){
System.out.println(meta.getColumnName(i) + ": " + resultSet.getString(meta.getColumnName(i)));
}
}
resultSet.close();
}
statement.close();
}
connection.close();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
publicstaticvoidtestdb(){
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
}catch(classnotfounde异常){
e、 printStackTrace();
}
试一试{
Connection Connection=DriverManager.getConnection(
“jdbc:mysql://localhost:3306/db-名称?user=user\u name&password=db\u password&usesl=false“;
if(连接!=null){
语句Statement=connection.createStatement();
if(语句!=null){
ResultSet ResultSet=statement.executeQuery(“从测试中选择*);
if(resultSet!=null){
ResultSetMetaData meta=resultSet.getMetaData();
int length=meta.getColumnCount();
while(resultSet.next())
{
对于(int i=1;i)开始时,Class.forName()
不需要JDBC连接字符串,而是JDBC驱动程序类名。如果JDBC路径不在环境变量中,如何查找它?在示例代码中,JDBC url已硬编码,未在任何环境变量中搜索上面的链接无效。H
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/db-name?user=user_name&password=db_password&useSSL=false
public static void testDB(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db-name?user=user_name&password=db_password&useSSL=false");
if (connection != null) {
Statement statement = connection.createStatement();
if (statement != null) {
ResultSet resultSet = statement.executeQuery("select * from test");
if (resultSet != null) {
ResultSetMetaData meta = resultSet.getMetaData();
int length = meta.getColumnCount();
while(resultSet.next())
{
for(int i = 1; i <= length; i++){
System.out.println(meta.getColumnName(i) + ": " + resultSet.getString(meta.getColumnName(i)));
}
}
resultSet.close();
}
statement.close();
}
connection.close();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}