2016-05-26 57 views
1

JSP頁面構建簡單的下面是一個簡單的代碼來顯示wheather數據庫連接或
not.I我使用MySQL工作臺和netbeans.It是顯示未連接
和玻璃魚服務器顯示 代碼後顯示的一些警告。我的數據庫中,無法連接上的NetBeans

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 
+0

我只想說JSP中的Java腳本是錯誤的代碼。使用Servlet將結果傳遞給JSP並通過EL(表達式語言)進行打印。 對於你的問題,你應該添加玻璃魚的標籤。 – AxelH

+0

@AxelH有一點要提,<% and %>之間的代碼不是Java腳本代碼,它是名爲scriplets的java代碼。是的,在我們有EL庫的日子裏編寫腳本是一種不好的做法。 JavaScript!= Java。 – drgPP

+1

我沒有編寫javascript,而是使用java腳本,因爲當您將jsp寫入jsp標記之間的jsp時,這是調用scriptlet,我稱之爲Java腳本:PA位混淆,我承認,但是當我看到它時,我可以識別Java;) – AxelH

回答

0

試試這個也許是一個語法錯誤,還檢查是否有正確的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); 
} 
1

所有我會說在JSP編寫數據庫代碼首先是不code.All數據庫代碼和業務邏輯必須在servlet或普通的Java class.Jsp編寫好辦法是認爲,從servlet的處理後, ,爲了顯示輸出我們可以使用jsp。

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

警告升起,因爲你可能在與GlassFish的web.xml中的上下文根斜線(即/)(應該是在WAR的WEB-INF文件夾)。

你可以擺脫警告,如果你刪除前面的斜線因此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. 
+0

我在哪裏可以找到glassfish-web.xml。在我的項目名稱中有web頁面,其中有web-inf文件夾但沒有glassfish-web.xml – Andy