Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to check if a data exists from database

hi,

i have this code:

String sqlString = "select * from Customer where CustomerID = ?";
PreparedStatement pstmt = con.prepareStatement(sqlString);
pstmt.setString(1, "cust001");
ResultSet rs = pstmt.executeQuery();

i want to check if the customer id cust001 exists in the database. how can i do it? what do i need to do with the result set?

thanks,

cristine


Comments

  • webtekiewebtekie Member Posts: 2
    You are half way there. You got the data, now you need to 'move' that data from ResultSet into Vector, Array, etc...

    rs = stmt.executeQuery(sql);
    ResultSetMetaData rsmd = rs.getMetaData();
    int numOfCol = rsmd.getColumnCount();
    data = new Vector();
    while(rs.next()){
    record = new String[numOfCol];
    for(int i = 0; i < numOfCol; i++){
    record[i] = cString(rs.getString(i+1));
    }
    data.add(record);
    }

    Also, make sure you close ResultSet right after you got the data and moved it. Don't leave it open.

    : hi,
    :
    : i have this code:
    :
    : String sqlString = "select * from Customer where CustomerID = ?";
    : PreparedStatement pstmt = con.prepareStatement(sqlString);
    : pstmt.setString(1, "cust001");
    : ResultSet rs = pstmt.executeQuery();
    :
    : i want to check if the customer id cust001 exists in the database. how can i do it? what do i need to do with the result set?
    :
    : thanks,
    :
    : cristine
    :
    :
    :

Sign In or Register to comment.