使用 DriverManager 将 Java 连接到 WAMP 上的 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37440888/
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
Connect Java to MySQL on WAMP using DriverManager
提问by bloopiebloopie
I am trying to connect to a database on a WAMP server on the same device the computer is running. Below is the code from a tutorial on zetcode. I have copied it almost completely except for the SQL login details. I am getting an error, it is displayed below the code.
我正在尝试连接到计算机正在运行的同一设备上的 WAMP 服务器上的数据库。下面是 zetcode 教程中的代码。除了 SQL 登录详细信息外,我几乎完全复制了它。我收到一个错误,它显示在代码下方。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.corba.se.impl.util.Version;
public class TestDb {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/mltest";
String user = "mlcomponents";
String password = "color12";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
}
Error message I am receiving:
我收到的错误消息:
May 25, 2016 3:51:28 PM TestDb main
SEVERE: No suitable driver found for jdbc:mysql://localhost:3306/mltest
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mltest
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestDb.main(TestDb.java:21)
采纳答案by jsurf
I faced the same problem before, try to run this first:
我以前遇到过同样的问题,请先尝试运行:
Class.forName("com.mysql.jdbc.Driver");
This will make the driver to register itself.
这将使驱动程序自行注册。
Check for more information regarding the Drive Manager, MySQL Connector.
检查有关驱动器管理器MySQL 连接器的更多信息。
From previous link:
从上一个链接:
When you are using JDBC outside of an application server, the DriverManager class manages the establishment of connections. Specify to the DriverManager which JDBC drivers to try to make Connections with. The easiest way to do this is to use Class.forName() on the class that implements the java.sql.Driver interface.
当您在应用程序服务器之外使用 JDBC 时,DriverManager 类管理连接的建立。向 DriverManager 指定要尝试与其建立连接的 JDBC 驱动程序。最简单的方法是在实现 java.sql.Driver 接口的类上使用 Class.forName() 。
Edit: I am adding a java class called MySQLConnector that can connect to mysql (in localhost) based on your second question. See how I import the java.sql.DriverManager
编辑:我正在添加一个名为 MySQLConnector 的 java 类,它可以根据您的第二个问题连接到 mysql(在本地主机中)。看看我如何导入 java.sql.DriverManager
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLConnector {
//JDBC driver name and database URL
private String JDBC_DRIVER;
private String DB_URL;
//Database credentials
private String USER;
private String PASS;
private Connection conn;
public MySQLConnector(){
JDBC_DRIVER = "com.mysql.jdbc.Driver";
DB_URL = "jdbc:mysql://localhost/db_name?useUnicode=yes&characterEncoding=UTF-8";
USER = "root";
PASS = "123";
conn = null;
}
public void openConnection(){
try{
//Register JDBC driver
Class.forName(JDBC_DRIVER);
//Open a connection
System.out.print("Connecting to a selected database... ");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Success!");
}catch(Exception e){
//Handle errors for JDBC
e.printStackTrace();
}
}
public void closeConnection(){
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
System.out.println("Connection closed");
}
public Connection getConnection(){
return conn;
}
}
回答by vibhor vaish
First of all start Wamp on your pc, you dont need another class to make connection, use this code and provide the username and password of your wamp login, also this code is only for making a connection, add the above functionality yourself
首先在你的电脑上启动Wamp,你不需要另外的类来建立连接,使用这个代码并提供你的wamp登录的用户名和密码,这个代码仅用于建立连接,自己添加上述功能
public class Jdbcdemo {
public static void main(String[] args) {
try
{
//loading the jdbc driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
//get a connection to database
Connection myConn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctry","your username","your password");
//create a statement
Statement stmt=myConn.createStatement();
//execute sql query
ResultSet rs=stmt.executeQuery("select * from employee");
//process the result
while(rs.next())
{
System.out.println(rs.getString("name")+" = "+rs.getString(1));
}
}
catch(SQLException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}