java 如何使用java连接本地Mysql?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2621785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:03:56  来源:igfitidea点击:

How to use java to connect to local Mysql?

javamysql

提问by Bozho

Is it need install anything? And if need , I want to get a full step to follow. And a simple connection coding. Thank you

需要安装什么吗?如果需要,我想完成一个完整的步骤。和一个简单的连接编码。谢谢

回答by Bozho

See this tutorial.

请参阅本教程

In short:

简而言之:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost/dbName", "root", "secret");

You can also use DriverManager.registerDriver(..)instead of the Class.forName(..).

您也可以使用DriverManager.registerDriver(..)代替Class.forName(..).

Of course you will need to download the mysql-connection-x.jar(the jdbc driver) and put it on your classpath.

当然,您需要下载mysql-connection-x.jarjdbc 驱动程序)并将其放在您的类路径中。

回答by Venkat

First you have to download MySql Driver, and add it into your java library or class path. Then try this,

首先,您必须下载 MySql 驱动程序,并将其添加到您的 Java 库或类路径中。那就试试这个

 try
   {
    Class.forName("com.MySql.jdbc.Driver");
    String Url = "jdbc:MySql://localhost:3306/databaseName";
    connection=DriverManager.getConnection(Url);
    String query="Select * from TableName";
    resultSet=statement.executeQuery(query); 
    }catch(Exception ex){
   System.out.println("Connection Error");
  }

回答by Venkat

At the first line the "pagkage" have underlined, please let me know to solve it?

在第一行“pagkage”有下划线,请告诉我解决它?

package com.stardeveloper.example;

包 com.stardeveloper.example;

import java.sql.Connection;

导入 java.sql.Connection;

import java.sql.DriverManager;

导入 java.sql.DriverManager;

import java.sql.SQLException;

导入 java.sql.SQLException;

回答by duffymo

I'd refer to this:

我会参考这个:

http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

It'll have all the examples you need to connect to MySQL using Connector-J.

它将包含使用 Connector-J 连接到 MySQL 所需的所有示例。

回答by duffymo

Class.forName("com.MySql.jdbc.Driver");

Class.forName("com.MySql.jdbc.Driver");

The "Driver" is it using followinf step to make out?

“驱动程序”是使用 followinf 步骤来制作的吗?

?Click Control Panel in the Windows OS. ?Click Administrative Tools ?Click Data Sources (ODBC) ?In the form that appears, select Add ?In the next form select the driver from the list – for example SQL Server

?单击 Windows 操作系统中的控制面板。?单击管理工具 ?单击数据源 (ODBC) ?在出现的表单中,选择添加 ?在下一个表单中,从列表中选择驱动程序 – 例如 SQL Server

回答by Maddy

You can try using spring datasource. http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/jdbc/datasource/package-summary.html

您可以尝试使用 spring 数据源。http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/jdbc/datasource/package-summary.html

I have found it convenient. Also, if it's a large scale application consider connection pooling too.

我觉得很方便。此外,如果它是大型应用程序,请考虑连接池。