如何在spring启动应用程序中使用tomcat服务器创建jdbc连接池

时间:2017-09-26 19:50:34

标签: java mysql spring tomcat spring-boot

我做了一个简单的spring boot项目,我想访问mysql数据库。我正在使用tomcat连接池进行数据库池化。我在我的server.xml文件中提供了以下属性,该文件位于tomcat / conf文件夹中。

<Resource name="jdbc/MyLocalDB"
  global="jdbc/MyLocalDB"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/demoData"
  username="root"
  password="root"
  maxActive="100"
  maxIdle="20"
  minIdle="5"
  maxWait="10000"/>

并在tomcat / conf / context.xml中 -

<ResourceLink name="jdbc/MyLocalDB"
            global="jdbc/MyLocalDB"
            auth="Container"
            type="javax.sql.DataSource" />

这是我的主要Spring启动应用程序类文件 -

package com.tomcat.pool;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication
public class SpringJDBCConnectionPoolApp {

public static void main(String[] args) {
    SpringApplication.run(SpringJDBCConnectionPoolApp.class, args);
}


@Resource(name="jdbc/myLocalDB")
private DataSource dataSource;

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
    return new JdbcTemplate(dataSource);
}
}

webServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

@WebServlet(urlPatterns = "/*")
public class WelcomeServlet extends HttpServlet {

@Resource(name="jdbc/MyLocalDB")
private DataSource dataSource;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {

    Statement stmt = null;
    ResultSet rs = null;
    try {
        Connection con = dataSource.getConnection();
        stmt = con.createStatement();

        rs = stmt.executeQuery("select id, name from user");

        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html");
        out.write("<html>");
        out.write("<body>");

        out.write("<h3>Database Records</h3>");
        while(rs.next())  {
            out.write("id: " + rs.getInt("id") + " ");
            out.write("name: " + rs.getString("name") + " ");
            out.write("</br>");
        }

        //lets print some DB information
        out.write("<h3>Database Meta Data</h3>");
        DatabaseMetaData metaData = con.getMetaData();
        out.write("Database Product: " + 
 metaData.getDatabaseProductName() + "<br/>");
        out.write("Database Version: " + 
 metaData.getDatabaseMajorVersion() + "."
                + metaData.getDatabaseMinorVersion() + "<br/>");
        out.write("Database Driver: " + metaData.getDriverName() + "
 <br/>");
        out.write("Database Driver version: " + metaData.getDriverMajorVersion() + "."
                + metaData.getDriverMinorVersion() + "<br/>");
        out.write("Database user: " + metaData.getUserName());
        out.write("</html>");

    } catch (SQLException e) {
        e.printStackTrace();
    } finally{
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            System.out.println("Exception in closing DB resources");
        }
    }
}
}

但我无法访问数据库。你能指出这段代码有什么问题。

0 个答案:

没有答案