【问题标题】:how to set datasource in jsp?jsp中如何设置数据源?
【发布时间】:2014-11-23 16:40:47
【问题描述】:

我有数据源,代码如下:

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 中的用户名、密码和数据库名称呢?

【问题讨论】:

  • 无论如何,在您使用 JSP 打印之前,没有人能够看到您的用户名、数据源中的密码
  • 但他们可以访问我的 jsp 页面本身没有.. 通常将所有这些都保存在 jsp 中不是一个好习惯。从堆栈本身学到的

标签: java mysql jsp jdbc


【解决方案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

【讨论】:

    【解决方案2】:

    连接类

        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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2016-05-03
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多