JDBC各个类详解-Connection
Connection:数据库连接对象
功能:
获取执行sql的对象
Statement createStatement()
PreparedStatement prepareStatement(String sql)
管理事务:
开启事务:setAutoCommit(booelean autoCommit) :调用该方法设置参数为false,即开启事务
提交事务:commit();
回滚事务:rollback
JDBC各个类详解-Statement
1.执行sql
1.boolean execute(String sql):可以执行任意的sql了解
2.int executeUpdate(String sql):执行DML(insert、update、delete)语句、DDL(create、alter、drop)语句
返回值:影响的行数,可以通过这个影响的行数判断CML语句是否执行成功 返回值>0的则执行成功,反之,则失败。
3. ResultSet executeQuery(String sql):执行DQL(select)语句
JDBC各个类详解-ResultSet-基本使用
ResultSet:结果集对象,封装查询结果
next()游标向下移动一行
getXxx(参数):获取数据
Xxx:代表数据类型 如 : int getInt() , String getString();
参数:
int:代表列的编号,从1开始,如:getString(1)
String:代表列名称,如:getDoublr("balance");
public static void main(String[] args) throws Exception { Connection conn = null; Statement stat = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root"); //定义sql String sql = "select * from account"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql rs = stat.executeQuery(sql); //处理结果 //让游标向上移动一行 rs.next(); //获取数据 int id = rs.getInt(1); String name = rs.getString("name"); double balance = rs.getDouble(3); System.out.println(id + "-" + name + "-" + balance); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat != null) { try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
JDResultSetBC各个类详解-ResultSet-遍历结果集
Boolean next() : 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据),
如果是,则返回false,如果不是则返回true
注意:
使用步骤:
1.游标向下移动一行
2.判断是否有数据
3.获取数据
public static void main(String[] args) throws Exception { Connection conn = null; Statement stat = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root"); //定义sql String sql = "select * from account"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql rs = stat.executeQuery(sql); //处理结果 //循环判断游标是否是最后一行末尾 while (rs.next()) { //获取数据 int id = rs.getInt(1); String name = rs.getString("name"); double balance = rs.getDouble(3); System.out.println(id + "-" + name + "-" + balance); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat != null) { try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
标签:stat,JDBC,String,rs,throwables,ResultSet,Connection,sql,null From: https://www.cnblogs.com/yuzong/p/16788569.html