sql才是王道(jdbc备忘)
import java.sql.*;public class test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// 1. 注册驱动try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}// Mysql 的驱动// 先定义变量,后使用和关闭java.sql.Connection conn = null;// 数据库连接java.sql.Statement stmt = null;// 数据库表达式java.sql.ResultSet rs = null;// 结果集try {// 2. 获取数据库的连接conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/yiyao?characterEncoding=UTF-8","admin", "admin"); // root是用户名,密码为空// 3. 获取表达式stmt = conn.createStatement();// 执行插入数据的 SQL// stmt.execute("set names 'latin1'");// 4. 执行 SQLrs = stmt.executeQuery("select * from zycf_type");// 5. 显示结果集里面的数据while (rs.next()) {System.out.println("学生姓名=" + rs.getString("typename"));}// 执行删除数据的 SQL// stmt.executeUpdate("delete from Student");} catch (SQLException e) {e.printStackTrace();} finally {// 6. 释放资源,建议放在finally语句中确保都被关闭掉了try {rs.close();} catch (SQLException e) {}try {stmt.close();} catch (SQLException e) {}try {conn.close();} catch (SQLException e) {}}}}
页:
[1]