JSP 用于制作员工详细信息的数据库

velaa5lx  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(106)

在下面的文件中,我做了一些编码来建立一个jdbc连接。我正在使用一个xampp服务器。
ConnectionProvider.java

package com.employeerecord.helper;

import java.sql.*;

public class ConnectionProvider {
//method GetConnection
    private static Connection connection;
    public static Connection getConnection() {
        try {
            //if connection is null then only it get created
            if(connection==null) {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost/emprecord","root","");  //Driver for Connection
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}

在下面的代码中,我做了一个jsp页面的前端,并放置了一些虚拟值。但是我得到了一个错误 “ConnectionProvider无法解析”,并且没有连接。
index.jsp

<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>

<!-- All CSS -->

<link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
    crossorigin="anonymous">
<link href="CSS/style.css" rel="stylesheet">

<!-- All JS -->

<script
    src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"
    integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB"
    crossorigin="anonymous"></script>
<script 
    src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" 
    integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" 
    crossorigin="anonymous"></script>

</head>
<body>
<%  Connection connection =  ConnectionProvider.getConnection();
    out.println(connection);
    %>
<%@include file="navbar.jsp" %><br><br><br>
                    <!-- Table -->
    <div class="row justify-content-center">
        <div class="col-md-10">
        <h3>All Employees In Our Company Are Listed Here</h3>
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col"> EmployeeID </th>
                        <th scope="col"> Name </th>
                        <th scope="col"> Skills </th>
                        <th scope="col"> Address </th>
                        <th scope="col"> Gender </th>
                        <th scope="col"> Salary </th>
                        <th scope="col"> BirthDate </th>
                        <th scope="col"> Edit </th>
                        <th scope="col"> Delete </th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td>xyz</td>
                        <td>Java</td>
                        <td>abc</td>
                        <td>Male</td>
                        <td>40,000</td>
                        <td>11/11/0000</td>
                        <td>Edit</td>
                        <td>Delete</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

错误
我正在使用xampp. Erron I am Getting在jsp文件和mysql服务器之间建立连接

4uqofj5v

4uqofj5v1#

我认为您尚未导入ConnectionProvider,您应该添加它

<%@page import="com.employeerecord.helper.ConnectionProvider"%>
sqyvllje

sqyvllje2#

在您的index.jsp中,ConnectionManager没有导入。
我建议您不要在视图中使用Connection
我希望您能将视图和数据库区域分开。
下面是分离视图和数据库区域的参考。
https://www.quora.com/Why-are-separate-views-necessary-in-database-applications

相关问题