JAVA学习笔记027——Java操作MySQL数据库实例(PreparedStatement)

一、两种方式拥有的方法:

1.Statement操作数据库:
增删改:executeUpdate()
查询:excuteQuery(); 

ResulteSet:保存查询后的结果集 select * from xxx
 next():判断
 previous():true/false
 getXxx(字段名|位置):获取具体的字段值

2.PreparedStatement操作数据库:
public interface PrepareStatement extends Statement
说明preparestatement是statement的子接口,因此statement有的,preparestetement也有:
1.增删改:executeUpdate()
2.查询:excuteQuery();
另外,preparestatement还有以下常用方法:
赋值操作SetXxx();

二、Preparedstatement与Statement的区别和选择:
1.Statement流程;
sql
executeUpdate(sql)

2.PreparedStatement流程:
sql(可能存在占位符?)
在创建PreparedStatement对象是,将sql预编译
connection.prepareStatement(sql)
excuteUpdate()
setXxx()替换占位符?

推荐使用PrepareStatement:原因如下:
1.编码更加简单(避免字符串的拼接)。
String name ="zs";
int age=23

String sql=" ..."
stmt.executeUpdate(sql);

pstmt:
String sql="insert into .......values(?,?)";
connectionprepareStatement(sql);//预编译SQL
pstmt.setInt(1,name);
pstmt.setInt(2,age);

2.提高性能
需要重复增加100(批量处理)

3.安全(可有效防止sql注入)

sql注入:将客户输入的内容和开发人员的SQL语句混为一体
例如提示输入用户名:
任意‘ or 1=1 --  (黑客的输入)
提示输入密码:
任意值  (黑客输入)

分析:
select count(*) from login where uname='任意值‘ or 1=1 --’and upwd='任意值‘;
--是SQL的注释符,该语句相当于:
select count(*) from login where uname='任意值‘ or 1=1;

综上所述,开发推荐使用Preparedstatement

Preparedstatement代码记录:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.cj.protocol.Resultset;

public class PreparedStamentDemo{
	private static final String URL="jdbc:mysql://localhost:3306/lds?serverTimezone=UTC";
	private static final String USERNAME="root";
	private static final String PWD="prolific";
//  


	public static void update() {//增删改
		Connection connection = null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			//  a.导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");//加载具体的驱动类
			//      b.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//c.发送sql,执行(增、删、改、查)
			String sql="insert into student values(?,?,?,?) ";
			//String sql="update student set STUNAME='Gaiming' where stuno=2";
			//String sql="delete from student where stuno=4";
			pstmt=connection.prepareStatement(sql);		
			pstmt.setInt(1, 13);
			pstmt.setString(2,"Chenliuyi");
			pstmt.setInt(3,22);
			pstmt.setString(4,"5hao");
			int count = pstmt.executeUpdate();  //返回值标是增删改几条数据
			//查询是stmt.executeQuery(sql);
			
			//d.处理结果
			
			if (count>0){
				System.out.println("操作成功!");
			}
			
			
		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(Exception e){
	  e.printStackTrace();
		}
		finally{
			try {
				if (pstmt!=null) pstmt.close();   //对象.方法
				if (connection!=null) connection.close();			
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}
	}

	
	
	
	public static void query() {//查询功能
		Connection connection = null;
		PreparedStatement pstmt =null;
		ResultSet rs=null;
		try {
			//  a.导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");//加载具体的驱动类
			//      b.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//c.发送sql,执行(增、删、改、查)
			String sql="select stuno,stuname from student";
			pstmt=connection.prepareStatement(sql);
			rs=pstmt.executeQuery();
			//查询是pstmt.executeQuery(sql);
			//int count = pstmt.executeUpdate(sql);  //返回值标是增删改几条数据
			
			//d.处理结果
			/*
			if (count>0){
				System.out.println("操作成功!");
			}
			*/
			while(rs.next()) {
//				int sno=((ResultSet) rs).getInt("stuno");
//				String sname=rs.getString("stuname");
				int sno=rs.getInt(1);
				String sname=rs.getString(2);
				System.out.println(sno+"------"+sname);
				
			}
			
		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(Exception e){
	  e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null) rs.close();
				if (pstmt!=null) pstmt.close();   //对象.方法
				if (connection!=null) connection.close();			
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	

	public static void main(String[] args){
		update();
		query();
		
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42844704/article/details/107192254