2016-05-26 8 views
1

上でJSPページの構築の簡単な接続取得されていない次のデータベースが接続されているか
not.Iは、MySQLのワークベンチを使用していますが、netbeans.Itが
と接続されていない表示されてwheatherを示すために簡単なコードですglassfishサーバーは、コード の後に表示されるいくつかの警告を表示しています。私のデータベースは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スクリプトが悪いコードであると言いたいと思います。サーブレットを使用して結果をJSPに渡し、EL(Expression Langage)で印刷します。 問題がある場合は、glassfishのタグを追加する必要があります。 – AxelH

+0

@AxelH言及しなければならないことの一つは、<% and %>のコードはJavaスクリプトコードではなく、それはscripletsという名前のJavaコードです。そして、はい、あなたがEL図書館を持っている時代には、スクリプレットを書くのは悪い習慣です。 JavaScript!= Java。 – drgPP

+1

私はjspタグ間のjspにJavaを書くときにスクリプトレットを呼び出すので、javascriptではなくJavaスクリプトを書いています。私はその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に良い方法ではないと言うだろうまず第一は、サーブレットまたはプレーンJava class.Jspで記述する必要がありますが、サーブレットから処理した後、図、 jspを使用できる出力を表示します。あなたはおそらく(WARのWEB-INFフォルダにする必要があります)のglassfish-web.xmlのあなたのコンテキスト・ルートにスラッシュを(すなわち、/)があるので

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

は、警告が発生します。

あなたの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-infフォルダがありますがglassfish-web.xmlはありません。 – Andy

関連する問題