如何在jsp中设置数据源?

时间:2014-09-29 10:06:41

标签: java mysql jsp jdbc

我有数据源。代码如下:

public class DataSource {
    Connection connection = null;
    BasicDataSource bdsource = new BasicDataSource();

    public DataSource() {
        bdsource.setDriverClassName("com.mysql.jdbc.Driver");
        bdsource.setUrl("jdbc:mysql://localhost:3306/databaseName");
        bdsource.setUsername("UserName");
        bdsource.setPassword("PassWord");

    }

    public Connection createConnection() {

        Connection con = null;

        try {

            if (connection != null) {
                System.out.println("Can't create a new connection");
            } else {
                con = bdsource.getConnection();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return con;
    }
}

我需要在jsp上显示数据库中的一些数据。那么我怎么能在jsp中设置数据源而不让世界知道jsp中的用户名,密码和数据库名呢?

2 个答案:

答案 0 :(得分:1)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>JSTL sql:setDataSource Tag</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/TEST"
     user="user_id"  password="mypassword"/>

<sql:query dataSource="${snapshot}" sql="..." var="result" />

</body>
</html>

更多详情请参阅link

答案 1 :(得分:0)

连接类

    public class ConnectBean {
    private Connection con;
    private static ConnectBean instance;
    private ConnectBean() throws Exception {
        try {
            String driver = "oracle.jdbc.OracleDriver";
            Class.forName(driver).newInstance();
            String url = "jdbc:oracle:thin:@10.0.1.1:1521:base";
            String user = "user";
            String password = "pass";            
            con = DriverManager.getConnection(url,user,password);

        } 
        catch (ClassNotFoundException e) {
            throw new Exception(e);
        }
        catch (SQLException e) {
            throw new Exception(e);
        }
    }
    public static synchronized  ConnectBean getInstance () throws Exception {
        if (instance == null) {
            instance = new ConnectBean();
        }
        return instance;         
    }
    public Connection getConnection() {
        return con;
    }
}

JSP页面

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>

<h2 align="center"><font color="#FF00FF"><strong>Select query in JSP</strong></font></h2>
<table align="center" cellpadding="4" cellspacing="4">
<tr>

</tr>
<tr bgcolor="#008000">
<td><b>Id</b></td>
<td><b>Name</b></td>
</tr>
<%
    Statement stmt = evg.oracle_jta.connection.ConnectBean.getInstance().getConnection().createStatement();
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM your_table");
    out.println("<html><body><h2>The Select query has following results : </h2>");

    while (resultSet.next()) {
        out.println("<tr bgcolor='#8FBC8F'>");
        out.println("<td>"+resultSet.getString("id") + "</td>");
        out.println("<td>"+resultSet.getString("name") + "</td>");
        out.println("</tr>");
    }    
%>  
</table>