用JSP连接数据库

时间:2014-08-18 23:43:30

标签: mysql jsp

请我连接到我的数据库,我已经添加了mysql连接器到eclipse但我无法连接到我的基地帮助我,谢谢

    <% try {
String connectionURL = "jdbc:mysql://localhost:3306/grp_asso";
Connection connection = null; 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
connection = DriverManager.getConnection(connectionURL,"root","");
if(!connection.isClosed())
     out.println("Successfully connected");
connection.close();}catch(Exception ex){
out.println("Unable to connect to database");} %>

1 个答案:

答案 0 :(得分:3)

这是我编写的用于连接数据库的java类。您需要将数据库添加到servlet容器XML配置文件中 - 如果您使用的是Tomcat(可能是JBoss,Glassfish和Spring都使用的话,可能是WEB-INF / web.xml和META-INF / context.xml),以防万一你正在使用其中任何一种。)

package your_package;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.NamingException;

public class ConnectionPool {
    private Connection connection = null;
    private javax.sql.DataSource dataSource = null;
    private javax.naming.Context envContext  = null;
    private javax.naming.Context initContext = null;        
    private String envContextDataSource = "jdbc/DataSourceName";
    private String envConextLookup = "java:/comp/env";

    public ConnectionPool() {
        super();
    }

    private void lookupDataSource() throws NamingException {
            this.initContext = new javax.naming.InitialContext();
            this.envContext  = (javax.naming.Context)this.initContext.lookup(this.envConextLookup);
            this.dataSource = (javax.sql.DataSource)this.envContext.lookup(this.envContextDataSource);
    }

    public void openConnection() throws SQLException, NamingException {

            if (this.dataSource == null) { lookupDataSource(); }
            this.connection = dataSource.getConnection();
    }

    public void closeConnection() {
        try {
            if (this.connection != null) {
                this.connection.close();
                this.connection = null;
            }
        } catch (SQLException e) {
            this.connection = null;
        }

    }

    public Connection getConnection() {
        return connection;
    }
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    public javax.sql.DataSource getDataSource() {
        return this.dataSource;
    }
    public void setDataSource(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
    }
    public javax.naming.Context getEnvContext() {
        return this.envContext;
    }
    public void setEnvContext(javax.naming.Context envContext) {
        this.envContext = envContext;
    }
    public javax.naming.Context getInitContext() {
        return this.initContext;
    }
    public void setInitContext(javax.naming.Context initContext) {
        this.initContext = initContext;
    }
    public String getEnvContextDataSource() {
        return envContextDataSource;
    }
    public void setEnvContextDataSource(String envContextDataSource) {
        this.envContextDataSource = envContextDataSource;
    }
    public String getEnvConextLookup() {
        return envConextLookup;
    }
    public void setEnvConextLookup(String envConextLookup) {
        this.envConextLookup = envConextLookup;
    }
}

在WEB-INF / web.xml中,您需要引用数据源:

 <resource-ref>
    <description>DataSourceName</description>
    <res-ref-name>jdbc/DataSourceName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref> 

在META-INF / context.xml中,您需要将数据源链接到数据库。

将“name”,“username”,“password”和“url”标记更改为您配置的正确值 - 我提供了示例值。如果您只是测试,则用户名可能是root,密码为空。

<?xml version="1.0" encoding="utf-8"?>
<Context>
    <Resource name="jdbc/DataSourceName" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="username" 
              password="password"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/database_name?autoReconnect=true"
              validationQuery="select 1"
              maxActive="1000" 
              maxIdle="10"/>
</Context>