1

現在、JSP の JDBC で問題が発生しています。ユーザー名/pass ext を MySQL DB に挿入しようとしています。エラーや例外は発生していませんが、DB には何も挿入されていません。以下は私のコードです。どんな助けでも大歓迎です。

<%
String uname=request.getParameter("userName");
String pword=request.getParameter("passWord");
String fname=request.getParameter("firstName");
String lname=request.getParameter("lastName");
String email=request.getParameter("emailAddress");
%>


<%
try{
    String dbURL = "jdbc:mysql:localhost:3306/assi1";
    String user = "root";
    String pwd = "password";
    String driver = "com.mysql.jdbc.Driver";

    String query = "USE Users"+"INSERT INTO User (UserName, UserPass, FirstName, LastName, EmailAddress) " +
                   "VALUES ('"+uname+"','"+pword+"','"+fname+"','"+lname+"','"+email+"')";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(dbURL, user, pwd);
    Statement statement = conn.createStatement();
    statement.executeUpdate(query);

    out.println("Data is successfully inserted!");
    }
    catch(SQLException e){
    for (Throwable t : e)
        t.printStackTrace();
    }
       %>

ここにDBスクリプト:

CREATE DATABASE Users;

use Users;

CREATE TABLE User (
UserID INT NOT NULL AUTO_INCREMENT,
UserName VARCHAR(20),
UserPass VARCHAR(20),
FirstName VARCHAR(30),
LastName VARCHAR(35),
EmailAddress VARCHAR(50),
PRIMARY KEY (UserID)
);
4

1 に答える 1

2

First of all, you don't need to use the USE statement in your SQL, since the database was already selected in the connection URL. Even if you use the USE statement, it should not be glued to the INSERT statement. So, in your dbURL, you need to set the apropriate db:

String dbURL = "jdbc:mysql://localhost:3306/Users";

Note that your URL was wrong too. You need two slashed after the second colon char. And you need to change your query variable to:

String query = "INSERT INTO User (UserName, UserPass, FirstName, LastName, EmailAddress) " + 
               "VALUES ('"+uname+"','"+pword+"','"+fname+"','"+lname+"','"+email+"')";

I hope your code is in this state because you are starting to learn Java Web development (I saw the homework tag). In the real world, this is not the way to develop Java web applications, since scriptlets (code between <% and %> and other variations) are not used anymore and a lot of other things needs to be improved, as the way you are dealing with JDBC.

Edit: Here is your fixed code:

<%
    Connection conn = null;

    try {

        String uname=request.getParameter("userName");
        String pword=request.getParameter("passWord");
        String fname=request.getParameter("firstName");
        String lname=request.getParameter("lastName");
        String email=request.getParameter("emailAddress");

        String dbURL = "jdbc:mysql://localhost:3306/Users";
        String user = "root";
        String pwd = "password";
        String driver = "com.mysql.jdbc.Driver";

        String query = "INSERT INTO User (UserName, UserPass, FirstName, LastName, EmailAddress) " +
                       "VALUES ('"+uname+"','"+pword+"','"+fname+"','"+lname+"','"+email+"')";
        Class.forName(driver);
        conn = DriverManager.getConnection(dbURL, user, pwd);
        Statement statement = conn.createStatement();
        statement.executeUpdate(query);
        statement.close();

        out.println("Data is successfully inserted!");

    } catch( ClassNotFoundException e ){
        System.err.println( "Database Driver class not Found!" );
        e.printStackTrace();
    } catch( SQLException e ){
        e.printStackTrace();
    } finnaly {
        try {
            if ( conn != null ) {
                conn.close();
            }
        } catch ( SQLException e ) {
            System.err.println( "Problems when closing connection" );
            e.printStackTrace();
        }
    }
%>

I think that this will work. As Lion said, consider using better solutions in your database code. I Think your teacher will teach these other things soon.

于 2012-08-17T02:22:04.303 に答える