首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的数据库不是在jsp页面上简单地连接到netbeans上的

问我的数据库不是在jsp页面上简单地连接到netbeans上的
EN

Stack Overflow用户
提问于 2016-05-26 11:00:49
回答 2查看 393关注 0票数 1

以下是显示wheather数据库已连接或

不,我正在使用mysql工作台,netbeans.It显示没有连接

而glassfish服务器正在显示一些警告,这些警告显示在代码之后。

代码语言:javascript
复制
 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-06 13:12:44

尝试一下,也许这是语法错误,也可以检查是否有正确的jar文件-

代码语言:javascript
复制
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);
 }
票数 0
EN

Stack Overflow用户

发布于 2016-05-26 11:27:11

首先,我要说用jsp编写数据库代码不是code.All的好方法,数据库代码和业务逻辑必须用servlet编写,或者普通的java class.Jsp是视图,经过servlet处理后,我们可以使用jsp显示输出。

代码语言:javascript
复制
This is just a warning, you can ignore it if you want.

引发此警告是因为您的上下文中可能有一个前导斜杠(即/) - glassfish-web.xml中的根(应该位于WAR的web.xml文件夹中)。

如果删除前面的斜杠,您可能会删除警告,因此glassfish-web.xml看起来类似于以下内容:

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37458958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档