Service Virtualization

Expand all | Collapse all

How to create JDBC mySQL connection in Script

Jump to Best Answer
  • 1.  How to create JDBC mySQL connection in Script

    Posted 08-07-2018 11:26 PM

    Hello,

    I am trying to set the connection for MySQL in the script: So far it is throwing me an error

    Below is the code. Can you please help me where I am doing mistake. For url and Pwd, I am giving credentials, but didn’t mention here

     

    import java.sql.*;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import javax.sql.DataSource;

     

    String dbUrl ="avcmg-di.cua8asf8jbhp.us-west-2.rds.amazonaws.com :3306";

    String username = "avcmg_user"; 

    String password = "directv1" ;

     

     

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("dbUrl","username", "password");

    Statement stmt=con.createStatement();            

    String query ="select guide_ccid from av_channels";

    ResultSet rs= stmt.executeQuery(query);



  • 2.  Re: How to create JDBC mySQL connection in Script

    Posted 08-08-2018 04:11 AM

    Hi Kuldeep,

     

    Could you please show us the error details?
    But by checking your script, I think that the value for dbUrl should be follow the style below:
    jdbc:mysql://[DBHOST]:[DBPORT]/[DBNAME]

    Then maybe
    String dbUrl ="jdbc:mysql://avcmg-di.cua8asf8jbhp.us-west-2.rds.amazonaws.com:3306/[DBNAME]";
    And I could not find [DBNAME] information in your script. Then you need to replace real your MySQL Database name in this part [DBNAME].

     

    Thank you,



  • 3.  Re: How to create JDBC mySQL connection in Script

    Posted 08-08-2018 06:43 PM

    I believe the issue is with the following line in your code:

     

    Connection con = DriverManager.getConnection("dbUrl","username", "password");

     

    You have mistakenly passed in the literal string values, "dbUrl", "username", "password", instead of passing in the String variables: dbUrl, username, password. In other words change it to the following (notice I have removed the quotation marks ("")):

     

    Connection con = DriverManager.getConnection(dbUrl, username, password);



  • 4.  Re: How to create JDBC mySQL connection in Script
    Best Answer

    Posted 08-08-2018 11:25 PM

    Hi Kuldeep,

     

       From your code, problem is in dbUrl string which you have mentioned. Since you are using mysql JDBC driver, connection string should be in format as jdbc:mysql://[DBHOST]:[DBPORT]/[DBNAME]

       Please change the connection string to above format and test it.

     

    Thanks & Regards

    Srikanth Gajawada