01.JDBC操作数据库-快速入门操作

/**
* 简单入门操作
* 注:先将mysql-connector-java-5.1.36.jar 构建 Build Path环境当中去
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//1.加载MySQL数据库驱动包
Class.forName("com.mysql.jdbc.Driver");
//2.连接MySQL数据库服务器
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java", "root", "root");
//3.创建执行语句对象
Statement st = conn.createStatement();
//4.执行语句
st.executeUpdate("INSERT INTO `student` SET name='刘诗华 77154113',age=28");
//5.释放资源
st.close();
conn.close();
}
    /**
* JDBC查询学生表
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//1.加载MySQL数据库驱动包
Class.forName("com.mysql.jdbc.Driver");
//2.连接MySQL数据库服务器
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java", "root", "root");
//3.创建执行语句对象
Statement st = conn.createStatement();
//4.执行语句
ResultSet rs = st.executeQuery("SELECT * FROM `student` ORDER BY id asc"); //rs当前是否指向数据表的某一行数据 如果有,则返true
while(rs.next())
{
System.out.println("学生姓名:"+rs.getString("name")+" 年龄:"+rs.getLong("age"));
}
//5.释放资源
rs.close();
st.close();
conn.close(); //学生姓名:刘诗华 年龄:28
//学生姓名:罗兰 年龄:27 }
上一篇:NIO Socket非阻塞模式


下一篇:如何在iOS上实现对HTTPS的支持(转)