JDBC学习第一站之Statement与PreparedStatement

来源:互联网 发布:零点有数科技 知乎 编辑:程序博客网 时间:2024/05/22 22:23

Statement:

通过类的名字,把类(元数据对象)加载到内存中,加载驱动,由于是对数据流做操作,一定要加异常处理,后面也是

// 通过类的名字,把类(元数据对象)加载到内存中Class.forName("com.mysql.jdbc.Driver");

创建一个数据库链接,getConnection方法中的第一个参数是指数据库类型和所操作数据库的名称,第二个参数是指用户名,第三个参数是指password<密码>

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/score", "root", "123456");
然后创建一个Statement对象

Statement stm = conn.createStatement();

一般执行SQL语句就是用,如果是查询则返回的是一张二维关系表,所以用ResultSet对象收集

<//执行增、删、改语句,返回int型,受影响的行数stm.executeUpdate(strSql);//返回一个ResultSet对象,其实就是一个表或者视图rst = stm.executeQuery(strSql);

ResultSet对象集,然后输出结果,如下

//输出结果while(rst.next()) {// 游标下移一行System.out.println(rst.getString("Sid") + "\t" + rst.getString("Sname"));}

如果对数据库的操作完成了,则需要在finally里面关闭前面创建的对象,对象销毁按照创建的栈来进行

finally {//打开是先打开Connection,再打开ResultSet//关闭是反的关闭if(rst != null) {try {rst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stm != null) {try {stm.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

总的代码:

package JDBC_Study;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.sql.ResultSet;public class MySqlDemo {public static void main(String[] args) {String strSql = "select * from student where ssex = '男';";//Connection conn = null;Statement stm = null;ResultSet rst = null ;try {// 通过类的名字,把类(元数据对象)加载到内存中Class.forName("com.mysql.jdbc.Driver");//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/score", "root", "123456");stm = conn.createStatement();//执行增、删、改语句,返回int型,受影响的行数stm.executeUpdate(strSql);//返回一个ResultSet对象,其实就是一个表或者视图rst = stm.executeQuery(strSql);//输出结果while(rst.next()) {// 游标下移一行System.out.println(rst.getString("Sid") + "\t" + rst.getString("Sname"));}} catch (ClassNotFoundException | SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//打开是先打开Connection,再打开ResultSet//关闭是反的关闭if(rst != null) {try {rst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stm != null) {try {stm.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}


PreparedStatement:

package JDBC_Study;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class PreparedStatementDemo {public static void main(String[] args) {Connection conn = null;PreparedStatement stmt = null;ResultSet rst = null ;String strSql = "select * from student where sid = ?";try {// 通过类的名字,把类(元数据对象)加载到内存中Class.forName("com.mysql.jdbc.Driver");//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/score", "root", "123456");//执行增、删、改语句,返回int型,受影响的行数//pStm.executeUpdate();//返回一个ResultSet对象,其实就是一个表或者视图stmt = conn.prepareStatement(strSql);stmt.setString(1, "100081");rst = stmt.executeQuery();//输出结果while(rst.next()) {// 游标下移一行System.out.println(rst.getString("Sid") + "\t" + rst.getString("Sname"));}} catch (ClassNotFoundException | SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//打开是先打开Connection,再打开ResultSet//关闭是反的关闭if(rst != null) {try {rst.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stmt != null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

Statement 与 PreparedStatement 的优点和缺点:


*PreparedStatement接口继承Statement接口
*PreparedStatement比普通的Statement对象使用起来更加灵活,更有效率 
提高了代码的可读性和可维护性
提高了SQL语句执行的性能
提高了安全性







0 0
原创粉丝点击