Class.forName(“com.mysql.jdbc.Driver”).newInstance()

前端 未结 4 849
盖世英雄少女心
盖世英雄少女心 2020-12-18 14:14

I am having this error on Netbeans 7.2, it says that ClassNotFoundexception and InstantationException. I am really stuck on this matter. Kindly help me.

prot         


        
相关标签:
4条回答
  • 2020-12-18 15:07

    All you need is Class.forName("com.mysql.jdbc.Driver")

    This acts like class loader and load your driver class for you. For that you need to add the corresponding jar file(which has the driver implementation). So download and add mysql-connector.jar in your class path.

    Note : If you are using Java 7 then there is no need to even add the Class.forName("com.mysql.jdbc.Driver") statement.Automatic Resource Management (ARM) is added in JDBC 4.1 which comes by default in Java 7.

    0 讨论(0)
  • 2020-12-18 15:08

    What about this simple way?!

    java.sql.Driver d=new com.mysql.jdbc.Driver();
    

    I also wondered why do you connect to database with such this way?! It's better let server manage it.

    First config the context.xml (if you are using tomcat) like this:

    <context>
    <Resource name="_ds" auth="Container" type="javax.sql.DataSource"
                   maxActive="128" maxIdle="32" username="_admin" password="qwerty" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://127.0.0.1:3306/dbname"/>
    </context>
    

    Then, simple get a connection from this resource in servlet/etc, like this:

    public void init() {
        try {
            _ds = (DataSource) InitialContext.lookup("java:/comp/env/_ds");
        } catch (Exception ex) {
        }
    }
    
    private javax.sql.DataSource _ds;
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try {
            /*String driver = "com.mysql.jdbc.Driver";
            con = null;
            String username = "";
            String password = "";
    
            Class.forName("com.mysql.jdbc.Driver").newInstance();
    
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "password");*/
            Connection con=_ds.getConnection();
            Statement st = con.createStatement();
            ResultSet mar = st.executeQuery("SELECT * FROM table");
    
    
            Gson gson = new GsonBuilder().create();
            response.setContentType("application/json");  
            response.setCharacterEncoding("utf-8"); 
            con.close();
        } catch (SQLException e) {
            String message = e.getMessage();
        }
    

    By the way, don't forget to compy the MySQL JDBC driver jar-file in <CATALINA_BASE>/lib folder.

    0 讨论(0)
  • 2020-12-18 15:12

    You might want to solve a bigger problem. You ought not enter configuration data such as database connection information directly in your servlet.

    Are you using Tomcat? You can simply use JNDI. You will be able to change database details and drivers without having to recompile your servlet.

    Here is the Tomcat 7.0 JNDI Datasource HOW-TO shows various ways in which you can get a Connection to your database.

    On that page, you have a code example of how to get a Connection (Oracle 8i, 9i & 10g -> Part 3), and how to write a MySQL specific configuration.

    Make sure to download a correct MySQL jar and place it in your Tomcat's lib/ directory (or alternatively your WAR's WEB-INF/lib).

    0 讨论(0)
  • 2020-12-18 15:17

    You need to add mysqlconnector.jar file found here: ( http://dev.mysql.com/downloads/connector/j/ ) into your lib folder of your project. Include it with your project and then you can access your connection with database.

    Add that jar into the buildpath.

    0 讨论(0)
提交回复
热议问题