-1

I was trying the code beneath to establish the database connection, but it does not work.

string connstr="jdbc:odbc:DRIVER={MySQL ODBC 3.51 Driver};SERVER=SERVER_NAME;DATABASE=db_name;UID=userpwd;PWD=userpwd";
string connstr2="jdbc:odbc:DRIVER={{MySQL ODBC 3.51 Driver}};SERVER=SERVER_NAME;DATABASE=db_name;UID=userpwd;PWD=userpwd";

Connection con = DriverManager.getConnection(connstr);

As you can see, I tried adding "jdbc:odbc:" to odbc connection string and also try replacing double curly backet into just single.

What am I doing wrong here?

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
Nite
  • 443
  • 1
  • 6
  • 11
  • Are you on Windows or Linux...do you have the MySQL JDBC connector installed? https://dev.mysql.com/downloads/connector/j/ – Hackerman Jun 09 '17 at 17:43
  • Also did you check if your connection string is in the right format?, you can check some samples here http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver – Hackerman Jun 09 '17 at 17:44
  • Possible duplicate of [Connect Java to a MySQL database](https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database) – KevinO Jun 09 '17 at 17:44
  • And just for the record I did not downvote your question....I think it is a valid question, but, you did little to no research about the subject – Hackerman Jun 09 '17 at 17:45
  • WINDOWS. Exception is "java.sql.SQLException: No suitable driver found" – Nite Jun 09 '17 at 17:47
  • 1. I did not have Do I have to find and install JDBC connector for all types of database? (Sybase, Netezza, ...) However, still the same error after installing JDBC drive for mysql. I confirmed that I have the following in my CLASSPATH environemnt variable. (m:\jars\mysql-connector-java-5.1.42-bin.jar) 2. What string do I pass if I want to use one configured in "ODBC Data Source administrator" in windows? – Nite Jun 09 '17 at 17:54
  • http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver is helpful. My connection string is completely off. Thank you. – Nite Jun 09 '17 at 18:01
  • 1
    Bear in mind that Java's JDBC-ODBC Bridge is buggy, unsupported, and has been removed from Java 8. You really should use MySQL Connector/J. – Gord Thompson Jun 09 '17 at 18:33

1 Answers1

1

Here is one described more detail approach of many ways to do:

//STEP 1. Import required packages
import java.sql.*;

public class FirstExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
      //STEP 6: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main
}//end FirstExample
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
  • I tried to use jdbc driver. I have mysql-connector-java-5.1.42-bin.jar in my CLASSPATH variable. However, I get an exception when I execution the follwoing code?
     Class.forName("com.mysql.jdbc.Driver");
    – Nite Jun 09 '17 at 18:50