1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

JDBC - Insert Issue

Discussion in 'JSP' started by StrykeRXL, Aug 17, 2012.

  1. StrykeRXL

    StrykeRXL New Member

    Joined:
    Aug 17, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone. I am new and a new student to programming. I am having some issues right now with JDBC in JSP. I am trying to insert username/pass ext into my MySQL DB. I am not getting any error or exception, however nothing is being inserted into my DB either. Below is my code, any help would be greatly appreciated.

    Code:
        <%@page import="java.sql.*, java.util.*, javax.servlet.*, java.io.*, javax.servlet.http.*"%>
    <%
    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!");
            conn.close();
            }
            catch(SQLException e){
            for (Throwable t : e)
                t.printStackTrace();
            }
               %>
     
  2. StrykeRXL

    StrykeRXL New Member

    Joined:
    Aug 17, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Sorry, I would like to add also my DB Script.

    Code:
    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)
    );
     

Share This Page