我的数据库在netbeans上的jsp页面构建上没有简单连接

时间:2022-02-15 22:16:09

The following is a simple code to show wheather database is connected or
not.I am using mysql workbench and netbeans.It is showing not connected
and the glassfish server is showing some warnings which are shown after the code.

以下是显示天气数据库是否连接的简单代码。我正在使用mysql workbench和netbeans.It显示未连接,并且glassfish服务器显示一些警告,这些警告显示在代码之后。

 code

 <%@page import="java.sql.DriverManager"%>
 <%@page import="java.sql.Connection"%>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <%
    Connection conn = null;

    try{
         Class.forName("com.mysql.jdbc.Driver");
     conn =DriverManager.getConnection
     ("jdbc:mysql://localhost:3306/userpass?zeroDateTimeBehavior                                                          
             =convertToNull","root", "root");
         if(conn!=null)
         {
             out.print("connected to database successfully");
         }

      }catch(Exception e)
      {
       out.print("not connected to database");
    }

    %>
   </body>
   </html>

   <warning and info*/
   Info:   Loading application __admingui done in 7,651  ms
   Warning:   Context path from ServletContext:  differs from path from      
         bundle:/
  Info:   Redirecting to /index.jsf
  Info:   Admin Console: Initializing Session Attributes...
 Warning:   Could not open/create prefs root node Software\JavaSoft\Prefs at        
 root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
 Warning:   Cannot create update center Image for C:\Program   
 Files\glassfish->4.1.1; Update Center functionality will not be available       
 in Admin Console

2 个解决方案

#1


0  

Try this maybe it is a syntax error also check if you have correct jar file or not-

尝试这可能是一个语法错误也检查你是否有正确的jar文件 -

Connection conn = null;

try{
     Class.forName("com.mysql.jdbc.Driver");
 conn =DriverManager.getConnection
 ("jdbc:mysql://localhost:3306/userpass","root", "root"); //removed after userpass
     if(conn!=null)
     out.println("connected to database successfully");
     else
     out.println("not connected to database");

  }catch(Exception e)
  {
   out.println(e);
 }

#2


1  

First of all I will say writing database code in jsp is not good way to code.All the database code and business logic must be written in servlet or plain java class.Jsp is view, after processing from servlet , to show output we can use jsp.

首先我要说在jsp中编写数据库代码并不是代码的好方法。所有数据库代码和业务逻辑都必须用servlet或普通的java类编写.jsp是视图,从servlet处理后,显示输出我们可以使用JSP。

This is just a warning, you can ignore it if you want.

The warning is raised because you probably have a leading slash (i.e. /) in your context-root in glassfish-web.xml (should be in the WEB-INF folder of the WAR).

引发警告是因为您可能在glassfish-web.xml中的context-root中有一个前导斜杠(即/)(应位于WAR的WEB-INF文件夹中)。

You may get rid of the warning if you remove the leading slash so your glassfish-web.xml looks similar to this:

如果删除前导斜杠,则可以删除警告,以使glassfish-web.xml看起来类似于:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish     Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>SalutationApp-war</context-root>
 </glassfish-web-app>



I hope I helped u.

#1


0  

Try this maybe it is a syntax error also check if you have correct jar file or not-

尝试这可能是一个语法错误也检查你是否有正确的jar文件 -

Connection conn = null;

try{
     Class.forName("com.mysql.jdbc.Driver");
 conn =DriverManager.getConnection
 ("jdbc:mysql://localhost:3306/userpass","root", "root"); //removed after userpass
     if(conn!=null)
     out.println("connected to database successfully");
     else
     out.println("not connected to database");

  }catch(Exception e)
  {
   out.println(e);
 }

#2


1  

First of all I will say writing database code in jsp is not good way to code.All the database code and business logic must be written in servlet or plain java class.Jsp is view, after processing from servlet , to show output we can use jsp.

首先我要说在jsp中编写数据库代码并不是代码的好方法。所有数据库代码和业务逻辑都必须用servlet或普通的java类编写.jsp是视图,从servlet处理后,显示输出我们可以使用JSP。

This is just a warning, you can ignore it if you want.

The warning is raised because you probably have a leading slash (i.e. /) in your context-root in glassfish-web.xml (should be in the WEB-INF folder of the WAR).

引发警告是因为您可能在glassfish-web.xml中的context-root中有一个前导斜杠(即/)(应位于WAR的WEB-INF文件夹中)。

You may get rid of the warning if you remove the leading slash so your glassfish-web.xml looks similar to this:

如果删除前导斜杠,则可以删除警告,以使glassfish-web.xml看起来类似于:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish     Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>SalutationApp-war</context-root>
 </glassfish-web-app>



I hope I helped u.