0
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);
    }
}

I am facing a problem in compiling this function and this 7th line is i suppose a problem is there an syntax error.If so please tell me.Here i want to take the name of database as parameter received from calling function.When i gave the exact name of database instead of parameter it worked,but not after i passed parameter in connection line.

2 Answers 2

1

it should be

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

problem is

"+db+"

you have extra " mark

Sign up to request clarification or add additional context in comments.

Comments

0
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;
}

1 Comment

Actually i wanted to take the input of dbname from user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.