Results 1 to 6 of 6
  1. #1
    sharanya is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Not able to insert data into mysql table

    hi all I wrote a application in jsp for entering data into mysql table
    but the jsp form is accepting the data but its not inserted into table can any one give suggesstion regarding this

    Thanks in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Without any code all I can say is you're doing something wrong.

  3. #3
    sharanya is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default not able to connect to mysql database

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >

    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>

    <HTML>
    <HEAD>
    <TITLE>insert data using prepared statement </TITLE>
    </HEAD>

    <BODY bgcolor="#ffffcc">
    <font size="+3" color="green"><br>Welcome in JSP Tutorials,EJB Tutorial,JDBC Tutorials,Free Java Servlets Tutorials, WAP Tutorials, Spring Framework Tutorials, J2EE Tutorials, BioInformatics Tutorials, Java Server Faces Tutorials, Jboss Tutorials, Hibernate Tutorials, XML and MySQL Tutorials !</font>
    <FORM action="prepared_statement_query.jsp" method="get">
    <TABLE style="background-color: #ECE5B6;" WIDTH="30%" >


    <TR>
    <TH width="50%">Name</TH>
    <TD width="50%"><INPUT TYPE="text" NAME="name"></TD>
    </tr>
    <TR>
    <TH width="50%">City</TH>
    <TD width="50%"><INPUT TYPE="text" NAME="city"></TD>
    </tr>
    <TR>
    <TH width="50%">Phone</TH>
    <TD width="50%"><INPUT TYPE="text" NAME="phone"></TD>
    </tr>


    <TR>
    <TH></TH>
    <TD width="50%"><INPUT TYPE="submit" VALUE="submit"></TD>
    </tr>
    </TABLE>
    <%
    String name = request.getParameter("name");
    String city = request.getParameter("city");
    String phone = request.getParameter("phone");

    /* Create string of connection url within specified
    format with machine name,
    port number and database name. Here machine name id
    localhost and database name is student. */
    String connectionURL = "jdbc:mysql://localhost:3306/student";

    // declare a connection by using Connection interface
    Connection connection = null;

    // declare object of Statement interface that uses for
    executing sql statements.
    PreparedStatement pstatement = null;

    // Load JBBC driver "com.mysql.jdbc.Driver"
    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    int updateQuery = 0;


    // check if the text box is empty
    if(name!=null && city!=null && phone!=null){

    // check if the text box having only blank spaces
    if(name!="" && city!="" && phone!="") {

    try {
    /* Create a connection by using getConnection()
    method that takes parameters of string type
    connection url, user name and password to connect
    to database. */
    connection = DriverManager.getConnection
    (connectionURL, "root", "root");

    // sql query to insert values in the secified table.
    String queryString = "INSERT INTO stu_info(Name,
    Address,Phone) VALUES (?, ?, ?)";

    /* createStatement() is used for create statement
    object that is used for
    sending sql statements to the specified database. */
    pstatement = connection.prepareStatement(queryString);
    pstatement.setString(1, name);
    pstatement.setString(2, city);
    pstatement.setString(3, phone);
    updateQuery = pstatement.executeUpdate();

    if (updateQuery != 0) { %>
    <br>
    <TABLE style="background-color: #E3E4FA;"
    WIDTH="30%" border="1">
    <tr><th>Data is inserted successfully
    in database.</th></tr>
    </table>
    <%
    }
    }
    catch (Exception ex) {
    out.println("Unable to connect to batabase.");


    }
    finally {
    // close all the connections.
    pstatement.close();
    connection.close();
    }
    }
    }
    %>
    </FORM>
    </body>
    </html>



    I have created a database and table wit stu_info in my database

  4. #4
    sharanya is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    sorry this is not my code

  5. #5
    sharanya is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    i have created a login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

    <%

    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPor t()+path+"/";

    %>



    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>

    <head>

    <script language="javaScript">

    function submitMe(){

    var userName = document.frmLogin.txtUserName;

    var passWord = document.frmLogin.txtUserName;

    document.frmLogin.action = "Welcome.jsp?username="+userName+"&password="+pass Word;

    document.frmLogin.submit();

    }

    </script>

    </head>



    <body>

    <form name="frmLogin">

    UserName : <input name="txtUserName"></input>

    Password : <input type="password" name="txtPassWord"></input>

    <input type="button" name="btnSubmit" value="Login" onClick="submitMe()"></input>

    </form>

    </body>

    </html>

    this is the code for that.It redirects to the welcome.jsp page

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

    <%@page import="com.ls.auth.ValidateLogin"%>

    <%

    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPor t()+path+"/";

    %>



    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>

    <head>



    </head>



    <body>

    <%

    String strUserName = request.getParameter("user");

    String strPassWord = request.getParameter("password");

    ValidateLogin vLogin = new ValidateLogin();

    boolean blnVal = vLogin.validate(strUserName,strPassWord);

    if(blnVal)

    {

    if(strUserName.equals(request.getParameter("user") ) &&

    strPassWord.equals(request.getParameter("password" )));

    }

    else

    {%>

    <h1> User Not Found</h1>



    <%}

    %>

    </body>

    </html>

    and then wrote a servlet class

    package com.ls.auth;



    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.Statement;



    public class ValidateLogin {

    public boolean validate(String strUserNm,String strPassword){

    try{

    String strUrl = "jdbc:mysql://localhost/database1";

    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    Connection con = DriverManager.getConnection(strUrl, "root", "notallowed");

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("Select * from user");/* where " +

    "user="+strUserNm+" and PassWord = "+strPassword);*/

    while(rs.next()){

    return true;

    }

    return false;

    }

    catch(Exception e){

    e.printStackTrace();

    return false;

    }

    }

    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    First, use code tags.
    Second, JSP pages really shouldn't have code like that in them. That's what servlets are for.

    Finally, a load of that seems to be to do with logging in...which strikes me as irrelevant to the question you asked. Which is the bit that's not working?

Similar Threads

  1. Replies: 1
    Last Post: 06-08-2010, 09:58 AM
  2. How to insert DATE into mysql using JSP?
    By sivakumar_sakam in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-27-2009, 07:52 PM
  3. Replies: 0
    Last Post: 02-28-2009, 11:16 AM
  4. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  5. Replies: 5
    Last Post: 10-20-2008, 06:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •