Mysql database Exception

后端 未结 2 1982
梦毁少年i
梦毁少年i 2021-01-15 19:30

Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from java.sql.Statement to com.mysql.jdbc.Statement<

2条回答
  •  佛祖请我去吃肉
    2021-01-15 20:02

    Wrong classes

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    

    should be

    import java.sql.Connection;
    import java.sql.Statement;
    

    In fact, java decouples everything from a specific database engine. One never should need an import of MySQL (or ProgressSQL or ...) classes.

    To have those classes available at run-time, the first thing after the try, before getting the connection would be:

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

    This technique would allow reading all strings from a configuration file, and writing database independent code.


    Missing: conn = ...

    conn = DriverManager.getConnection(CONN_STR, userName, userpwd);
    

提交回复
热议问题