|
I just realized I don't have a JDBC PreparedStatement example out here, so I'm adding one here.
//
// A simple JDBC/SQL PreparedStatement example.
//
public void addUser(User user, Connection conn)
throws SQLException
{
String query = "INSERT INTO Users ("
+ " user_id,"
+ " username,"
+ " firstname,"
+ " lastname,"
+ " companyname,"
+ " email_addr,"
+ " want_privacy ) VALUES ("
+ "null, ?, ?, ?, ?, ?, ?)";
try {
// kind of boring, this just uses Strings
PreparedStatement st = conn.prepareStatement(query);
st.setString(1, user.getName());
st.setString(2, user.getFirstName());
st.setString(3, user.getLastName());
st.setString(4, user.getCompanyName());
st.setString(5, user.getEmail());
st.setString(6, user.getPrivacy());
st.executeUpdate();
st.close();
}
catch (SQLException se)
{
// log exception
throw se;
}
}
FWIW, a lot of times I put stuff out here because of my own bad memory. I'd probably be better off using the same IDE all the time and creating some templates, but this also works. It's pretty easy to come here and search for JDBC or PreparedStatement and find what I need.
|