public static void main(String[] args) throws SQLException { ? ? getconn();
? ? String selectsql="select * from info"; (所有查询代码为: select * from info where tel like '?%'; select * from info where sex=?; select * from info where tel like '?%'; select * from info where sex=?; ) ? ? Object[] obj = {};
? ?select(selectsql,obj);
? ? demonext(); ? ? democlose(); } ?
private static void democlose() throws SQLException { ? ? resultSet.close(); ? ? pstat.close(); ? ? conn.close(); } private static void demonext() throws SQLException { ? ? while (resultSet.next()){ ? ? ? ? System.out.println("id="+resultSet.getString("id") ? ? ? ? ? ? ? ? +" name="+resultSet.getString("name") ? ? ? ? ? ? ? ? +" tel="+resultSet.getString("tel") ? ? ? ? ? ? ? ? +" sex="+resultSet.getString("sex")); ? ? } } private static void select(String selectsql,Object[] obj) throws SQLException { ? ? pstat = conn.prepareCall(selectsql); ? ?for (int i = 0; i < obj.length; i++) { ? ? ? ? pstat.setObject(i+1,obj[i]); ? ?} ? ? resultSet =pstat.executeQuery(); } ? ?
|