的Class.forName(QUOT; com.mysql.jdbc.Driver&QUOT)的newInstance()。 [英] Class.forName("com.mysql.jdbc.Driver").newInstance()

查看:124
本文介绍了的Class.forName(QUOT; com.mysql.jdbc.Driver&QUOT)的newInstance()。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Netbeans 7.2上遇到这个错误,它说ClassNotFoundexception和InstantationException。我真的被这个问题困住了。请帮助我。

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.

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");
        Statement st = con.createStatement();
        ResultSet mar = st.executeQuery("SELECT * FROM table");


        Gson gson = new GsonBuilder().create();
        response.setContentType("application/json");  
        response.setCharacterEncoding("utf-8"); 

    } catch (SQLException e) {
                String message = e.getMessage();
    }


推荐答案

这个简单的方法怎么样? !

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.

首先配置 context.xml (如果你使用的是tomcat)就像这样:

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>

然后,简单地从servlet / etc中获取此资源的连接,如下所示:

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();
    }

顺便说一句,不要忘记编写MySQL JDBC驱动程序jar-文件在< CATALINA_BASE> / lib 文件夹中。

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

这篇关于的Class.forName(QUOT; com.mysql.jdbc.Driver&QUOT)的newInstance()。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆