gpt4 book ai didi

java - 如何以database为参数连接mysql数据库服务器

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

import java.sql.*;
class ConnectSql
{
static Connection cont(String db)throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db+","root","xyz" );
return(con);
}
}

我在编译此函数时遇到问题,第 7 行是我认为存在语法错误的问题。如果是这样,请告诉我。这里我想将数据库名称作为从调用函数接收的参数。当我给出了数据库的确切名称,而不是它起作用的参数,但在我在连接行中传递参数之后就没有了。

最佳答案

应该是

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db,"root","xyz" );

问题是

"+db+"

你有额外的"标记

关于java - 如何以database为参数连接mysql数据库服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381242/

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