gpt4 book ai didi

java - 从 Eclipse 访问 Wamp 服务器数据库

转载 作者:行者123 更新时间:2023-11-29 06:17:56 25 4
gpt4 key购买 nike

我想使用 Java 访问我计算机中的 Wamp 服务器数据库。我的代码如下:

public static void main(String[] args)
{
try
{
// create our mysql database connection
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/deneme";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");

// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM users";

// create the java statement
Statement st = conn.createStatement();

// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);

// iterate through the java resultset
while (rs.next())
{
int id = rs.getInt("id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
Date dateCreated = rs.getDate("date_created");
boolean isAdmin = rs.getBoolean("is_admin");
int numPoints = rs.getInt("num_points");

// print the results
System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints);
}
st.close();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}

在此之后,它给我这个错误:

Got an exception! 
com.mysql.jdbc.Driver

如何将我自己电脑中的数据库连接到java?

谢谢。

最佳答案

尝试在/lib文件夹中添加mysql-connector-java-xxxx-bin.jar。
您可以从here下载。 .
然后右击 pproject properties --> Java Build Path --> External jar

关于java - 从 Eclipse 访问 Wamp 服务器数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com