import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
/*(1)通过jdbc 将数据库中表信息查询出来 (3分)
* (2)将文件读取出来存入数据库 要求如下:如果是女生,且分数 <90 分,则存入的分数+10,若大于90分,则以100分计算(6分)
* (3)将姓名中带小字的同学查询出来*/
public class zuoye1 {
public static void main(String[] args) throws SQLException {
Connection conn=Dbutils.getConnection();
String sql="select * from student";
PreparedStatement ps= conn.prepareStatement(sql);
ResultSet rs= ps.executeQuery();
while(rs.next()){
/*姓名 年龄 性别 分数 */
String sanme= rs.getString("sanme");
int age=rs.getInt("age");
String gender=rs.getString("gender");
int score=rs.getInt("score");
System.out.println("sanme =" +sanme + ",age ="+age + "gender="+gender+",socre ="+score);
}
//String sql2="select * from student where gender='女'";
// String sql3="update student set score = "+score+10+" where score<90";
String sql3 = "update student set score = 100 where gender='女'and score> 90";
//PreparedStatement ps2= conn.prepareStatement(sql2);
// ResultSet rs2= ps2.executeQuery();
PreparedStatement ps3= conn.prepareStatement(sql3);
int rs3=ps3.executeUpdate(sql3);
while(rs3>0){
System.out.println("修改成功");
}
ps3.close();
String sql4="select * from student where name like '%小%'";
PreparedStatement ps4= conn.prepareStatement(sql4);
ResultSet rs4= ps4.executeQuery();
while(rs4.next()){
/*姓名 年龄 性别 分数 */
String sanme= rs.getString("sanme");
int age=rs.getInt("age");
String gender=rs.getString("gender");
int score=rs.getInt("score");
System.out.println("sanme =" +sanme + ",age ="+age + "gender="+gender+",socre ="+score);
}
DbUtils.close(conn);
}
} |