如何将数据库作为参数连接到 mysql 数据库服务器
how to take database as parameter to connect to mysql database server
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 行我想语法有问题 error.If 所以请告诉 me.Here 我想将数据库名称作为参数从调用中收到 function.When 我给出了数据库的确切名称而不是它工作的参数,但是在我在连接线中传递参数之后没有。
应该是
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db,"root","xyz" );
问题是
"+db+"
你有额外的"
标记
Connection con=null;
String DB_URL = "jdbc:mysql://localhost:3306/";
String USER = "abc";//db user name
String PASS = "abc";//db password
public synchronized Connection getConnection(String dbname)
{
try
{
Class.forName("com.mysql.jdbc.Driver");//loading mysql driver
con = DriverManager.getConnection(DB_URL+dbname,USER,PASS);//connecting to mysql
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
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 行我想语法有问题 error.If 所以请告诉 me.Here 我想将数据库名称作为参数从调用中收到 function.When 我给出了数据库的确切名称而不是它工作的参数,但是在我在连接线中传递参数之后没有。
应该是
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db,"root","xyz" );
问题是
"+db+"
你有额外的"
标记
Connection con=null;
String DB_URL = "jdbc:mysql://localhost:3306/";
String USER = "abc";//db user name
String PASS = "abc";//db password
public synchronized Connection getConnection(String dbname)
{
try
{
Class.forName("com.mysql.jdbc.Driver");//loading mysql driver
con = DriverManager.getConnection(DB_URL+dbname,USER,PASS);//connecting to mysql
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}