1
import java.sql.*;
import java.util.*;

public class app1conn {
String url="jdbc:mysql://localhost:3306/test",uname,password;
Connection con;
PreparedStatement ps;

app1conn() throws SQLException{

   DriverManager.registerDriver(new com.mysql.jdbc.Driver());
   con= DriverManager.getConnection(url,"root","");

}

public void insert (String name,String email,String age,String dob)throws Exception{


     ps=con.prepareStatement(url);
    //ps=new con.PreparedStatement ("insert into info(name,email,age,dob) values(?,?,?,?)") ;
    ps.setString(1,name);
    ps.setString(2,email);
    ps.setString(3,age);
    ps.setString(4,dob);
    ps.executeUpdate();

    }
public Vector getData() throws SQLException{
    ps=con.prepareStatement("Select * from info");
    ResultSet rs=ps.executeQuery();
    ResultSetMetaData rms=rs.getMetaData();
    Vector data=new Vector();

    while(rs.next()){

        Vector temp=new Vector();
        temp.add(rs.getString(1));
        temp.add(rs.getString(2));
        temp.add(rs.getString(3));
        temp.add(rs.getString(4));
        temp.add(rs.getString(5));


        data.add(temp);

    }
    return data;
}

 public Vector getDataById(String id) throws SQLException{
    ps=con.prepareStatement("Select * from info where id=?");
    ps.setInt(1,Integer.parseInt(id));
    ResultSet rs=ps.executeQuery();
    ResultSetMetaData rms=rs.getMetaData();
    Vector data=new Vector();

    while(rs.next()){

        Vector temp=new Vector();
        temp.add(rs.getString(1));
        temp.add(rs.getString(2));
        temp.add(rs.getString(3));
        temp.add(rs.getString(4));
        temp.add(rs.getString(5));


        data.add(temp);

    }
    return data;
 }
    void update_info(String name,String email,String age,String dob,String id) throws SQLException{
        ps=con.prepareStatement ("update info set name=?,email=?,age=?,dob=? where id=?");
        ps.setString(1, name);
        ps.setString(2, email);
        ps.setString(3, age);
        ps.setString(4, dob);


        ps.executeUpdate();
    }

} 

i am trying to insert the data.. when i click on submit button..

it shows null as an output..

4
  • 1
    This doesn't look much like Javascript.. Commented Dec 28, 2013 at 8:58
  • why is tagged as javascript? Commented Dec 28, 2013 at 8:58
  • 1
    Where are the servlets? There is none in the code you provide... Commented Dec 28, 2013 at 9:18
  • how to compile something like "String url="jdbc:mysql://localhost:3306/test",uname,password;"? Commented Jun 14, 2014 at 7:38

2 Answers 2

1
String insertSQL = "insert into info(name,email,age,dob) values(?,?,?,?)";
PreparedStatement ps = dbConnection.prepareStatement(insertSQL);
    ps.setString(1,name);
    ps.setString(2,email);
    ps.setString(3,age);
    ps.setString(4,dob);
    ps.executeUpdate();

Or

String insertTableSQL = "INSERT INTO DBUSER"
        + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
        + "(?,?,?,?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "abc");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL stetement
preparedStatement .executeUpdate();

Refer to this article

Sign up to request clarification or add additional context in comments.

1 Comment

Refer to the article link I have provided, it's work. I have tried that one so many times.
0

Have you trace your code on which line you got null value???

Try this code instead of yours.

public boolean insert (String name,String email,String age,String dob){
 PreparedStatement preparedStatement;
 try{
 preparedStatement = con.prepareStatement("insert into info(name, email, age, dob) values(?,?,?,?)");
 preparedStatement.setString(1,name);
 preparedStatement.setString(2,email);
 preparedStatement.setString(3,age);
 preparedStatement.setString(4,dob);
 preparedStatement.executeUpdate();
 }
 catch (Exception e) {
    return false;
 }
   return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.