-3

I've trying to insert data in db using my sql. string from where to getting data and inserting in db.

String is :

s=
1 kalim khan NIL    
2 ajay sharma NIL   
3 navneet gupta  NIL    
4 amar kumar srivastava NIL 

I want that where spaces are more then 3 that should skiped and moved ahead

the code is following as:

StringTokenizer str = new StringTokenizer(htmlTableText);
   while(str.hasMoreElements()){
      for(int i=0;i<4;i++){
          int sno = Integer.parseInt(str.nextElement().toString());

        String fname = str.nextElement().toString();
        String lname = str.nextElement().toString();
        String price = str.nextElement().toString();
        Class.forName("com.mysql.jdbc.Driver");
        Connection  cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mandi","root","");
         //insert them into the database
         PreparedStatement ps=cn.prepareStatement("insert into commoditywise  values(?,?,?,?)");
         ps.setInt(1,sno);
         ps.setString(2, fname);
         ps.setString(3, lname);
         ps.setString(4, price);
            int j =ps.executeUpdate();
            if(j==1)
            {
            System.out.println("data inserted");
            }
            else
            {
            System.out.println("not inserted");
            }
      }
   }

Thanks in advance

2 Answers2

0

Maybe StringTokenizer's count method can help you?

int count = str.countTokens();
if (count > 4) {
   continue;
}

Otherwise I would just insert the first strings until I get to the last name.

0
ps.setInt(1,sno);
ps.setString(2,fname);
ps.setString(3,lname);
ps.setString(4,price);

There is some error in these lines,

Solution: Use DTO ( Setter and Getter method class / bean class) to get sno,fname,lname,price values

try this here 1.dtoclassname= your bean class ( setter and setter method class)
2.getSno() = is method for getting value.

ps.setInt(1,dtoclassname.getSno());

do above said operations for other fields.

JohnRose
  • 409
  • 5
  • 10