mysqljdbcsqlexceptionjdbc-odbc

when i want to insert data into mysql using JdbcOdbc Bridge driver, i get no database selected error,


i have created dsn correctly and have clear and simple code but i end up getting this error. the table structure is same as the program

   import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    public class Insert {
        public static void main(String[] args) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbc");
                Connection con=DriverManager.getConnection("jdbc:odbc:mysqldsn","root","filimon");
                Statement st=con.createStatement();
                int i=st.executeUpdate("insert into emp values(3,'cat')");
                if (i>=1) {
                    System.out.println("inserted successfully");
                } else {
                    System.out.println("failed");
                }
                st.close();
                con.close();
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }

I get java.sql.SQLException: [MySQL][ODBC 5.3(a) Driver][mysqld-5.7.13-log]No database selected


Solution

  • Error said that you haven't selected a database. But selection of the database would be somewhere in the ODBC configuration.

    So you can do two things : Either you fix your ODBC configuration to select a database, or (a better option) you can stop using ODBC and use the MySQL JDBC driver.