2016-07-15 1 views
0

ライブラリにApache tomcatを追加しました。私の接続はすべて正しいです。htmlページを通してsqlylogにデータを挿入したいと思います。私のコードで何が間違っているのですか?私は挿入クエリとそのファイルについて疑問を抱いています。netbeansを通じてSQLyogコミュニティにアクセスしてクエリを挿入するにはどうすればよいですか?私のコードはここにあります

public class myservlet extends HttpServlet { 

class dbconn { 

    Connection c1 = null; 
    Statement st = null; 
    ResultSet rs = null; 
    private final String ac; 
    private final String aaa; 

    dbconn() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher", "root", "abcde"); 

     } catch (Exception cnfe) { 
      System.out.println("Couldn't find the driver!"); 
      System.out.println("Couldn't connect: print out a stack trace and exit."); 
      System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected."); 
     } 
     try { 
      st = (Statement) c1.createStatement(); 
      System.out.println("Statement Created Successfully"); 
     } catch (SQLException se) { 
      System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected."); 

     } 
     if (c1 != null) { 
      System.out.println("Hooray! We connected to the database!"); 
     } else { 
      System.out.println("We should never get here."); 
     } 
     String query = "INSERT INTO teacher1 (id,name) " 
       + "VALUES (" + 1 + ", '" + aaa + "')";  // insert query is this 
    } 
} 

protected void processRequest(HttpServletRequest request, 
     HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try { 

     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet myservlet</title>"); 
     out.println("</head>"); 
     out.println("<body >"); 

     out.println("<h1>Servlet myservlet at " + request.getContextPath() + "</h1>"); 
     String name = request.getParameter("firstname"); 
     out.println("<h2>" + name + "</h2>"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } finally { 
     out.close(); 
    } 
} 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

    public String getServletInfo() { 
    return "Short description"; 
} 

index.jspを

<!DOCTYPE html> 
    <html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    </head> 
    <body> 
    <h1>Hello World!</h1> 

    <form action="myservlet" method="GET" > 

     First name:<br> 
     <input type="text" name="firstname"/> 
     <br> 
     Last name: 
     <br> 
     <input type="text" name="lastname" /> 
     <br><br> 
     <input type="submit" value="Submit"/> 
     </form> 
     </body> 
     </html> 
+1

あなたは私たちがはるかに上に行くために与えられていません。 SqlyogはTomcatサーブレットのようなSQLクライアントプログラムです。あなたの質問を編集してください。遅らさないでください。そうでないと、質問が閉鎖される可能性があります。 –

+0

あなたのコードはどこですか? –

+0

あなたはまずそれを変更しなければならないことがたくさんあります。 –

答えて

0

私は図書館でのApache Tomcatを追加し、すべての私の接続は、私は、HTMLページからsqlylogにデータを挿入する正しいです。私のコードで何が間違っているのですか?私は挿入クエリとそのファイルについて疑問を抱いています。

myservlet.java

import java.beans.Statement; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class myservlet extends HttpServlet { 

    class dbconn { 

     Connection c1 = null; 
     Statement st = null; 
     ResultSet rs = null; 
     private final String ac; 
     private final String aaa; 

     dbconn() { 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher", "root", "abcde"); 

      } catch (Exception cnfe) { 
       System.out.println("Couldn't find the driver!"); 
       System.out.println("Couldn't connect: print out a stack trace and exit."); 
       System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected."); 
      } 
      try { 
       st = (Statement) c1.createStatement(); 
       System.out.println("Statement Created Successfully"); 
      } catch (SQLException se) { 
       System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected."); 

      } 
      if (c1 != null) { 
       System.out.println("Hooray! We connected to the database!"); 
      } else { 
       System.out.println("We should never get here."); 
      } 
      String query = "INSERT INTO teacher1 (id,name) " 
        + "VALUES (" + 1 + ", '" + aaa + "')";  // insert query is this 
     } 
    } 

    protected void processRequest(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try { 

      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet myservlet</title>"); 
      out.println("</head>"); 
      out.println("<body >"); 

      out.println("<h1>Servlet myservlet at " + request.getContextPath() + "</h1>"); 
      String name = request.getParameter("firstname"); 
      out.println("<h2>" + name + "</h2>"); 
      out.println("</body>"); 
      out.println("</html>"); 
     } finally { 
      out.close(); 
     } 
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    public String getServletInfo() { 
     return "Short description"; 
    } 
} 

はindex.jspを

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 

     <form action="myservlet" method="GET" > 

      First name:<br> 
      <input type="text" name="firstname"/> 
      <br> 
      Last name: 
      <br> 
      <input type="text" name="lastname" /> 
      <br><br> 
      <input type="submit" value="Submit"/> 
     </form> 
    </body> 
</html> 
関連する問題