package com.imau.entity;


import java.sql.*;


public class testPrep {
public static void main(String[] args) {
// 声明Connection对象
Connection con;
// 数据库驱动
String DRIVER = "com.mysql.jdbc.Driver";
// 3306后面为数据库名字 
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
// 数据库密码
String password = "123456";
try {
// 注册驱动
Class.forName(DRIVER);
// 1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url, username, password);
if (!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
// 2.创建statement类对象,用来执行SQL语句!!



String sql = "select * from student";
//String insertsql ="insert into student values(2014123,'红尘','女',89,97);";
//String deletesql ="delete from student where id=2014123";
String updatesql = "update student set sex='女' where name='张三'";



PreparedStatement statement = con.prepareStatement(updatesql);
PreparedStatement statement2 = con.prepareStatement(sql);
// 3.ResultSet类,用来存放获取的结果集!!
int re = statement.executeUpdate();

System.out.println("插入了"+re+"行");
ResultSet rs = statement2.executeQuery();


System.out.println("----------------------------------");
System.out.println("执行结果如下所示:");
System.out.println("----------------------------------");
System.out.println(" id" + "\t" + " 姓名" + "\t" + "性别" + "\t" + "数学" + "\t" + "英语");
System.out.println("----------------------------------");


String sid = null;
String sname = null;
String ssex = null;
String smath = null;
String sEnglish = null;


while (rs.next()) {
sid = rs.getString("id");// 从表的第一列取数据
sname = rs.getString("name");
ssex = rs.getString("sex");
smath = rs.getString("math");
sEnglish = rs.getString("English");


System.out.println(sid + "\t" + sname + "\t" + ssex + "\t" + smath + "\t" + sEnglish);


}


rs.close();
con.close();
} catch (ClassNotFoundException e) {
// 数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
// 数据库连接失败异常处理
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
// System.out.println("数据库数据成功获取!!");
}
}
}