Java中PreparedStatement的用法是什么

发布时间:2021-08-13 10:21:23 作者:chen
来源:亿速云 阅读:322

这篇文章主要讲解了“Java中PreparedStatement的用法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java中PreparedStatement的用法是什么”吧!

PreparedStatement介绍

Java中PreparedStatement的用法是什么

PreparedStatement vs Statement

插入案例

PreparedStatement常用的方法:

void setObject(int parameterIndex, Object x, int targetSqlType)

Java中PreparedStatement的用法是什么

parameterIndex the first parameter is 1, the second is 2, …占位符参数索引是从1开始的
其余也是如此:

void setInt(int parameterIndex, int x)
void setLong(int parameterIndex, long x)
void setString(int parameterIndex, String x)
void setBlob (int parameterIndex, Blob x)
void setDate(int parameterIndex, java.sql.Date x, Calendar cal)

Java中PreparedStatement的用法是什么

执行操作:

Java中PreparedStatement的用法是什么

package com.atmf;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.junit.Test;

public class SumUP {
	@Test
	public void getConnection() {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			//1,加载配置文件
			InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
			Properties pr = new Properties();
			pr.load(is);
			
			//2,读取配置信息
			String user = pr.getProperty("user");
			String password = pr.getProperty("password");
			String url = pr.getProperty("url");
			String driverClass = pr.getProperty("driverClass");
			
			//3.加载驱动
			Class.forName(driverClass);
			
			//4,获取连接
			con = DriverManager.getConnection(url, user,password);
			
			String sql = "insert into customers(name,birth) value(?,?)";
			//预编译sql语句,得到PreparedStatement对象
			ps = con.prepareStatement(sql);
			
			//5,填充占位符
			ps.setString(1, "三明治");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse("2020-11-02");
			ps.setDate(2, new java.sql.Date(date.getTime()));
			
			//6,执行操作
			ps.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//7,关闭资源
			try {
				if(ps != null)
					ps.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(con != null)
					con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

配置信息:jdbc.properties文件
user=root
password=123456
url=jdbc:mysql://localhost:3306/students
driverClass=com.mysql.jdbc.Driver

执行结果:

Java中PreparedStatement的用法是什么

PreparedStatement实现对表数据的增删改查操作

感谢各位的阅读,以上就是“Java中PreparedStatement的用法是什么”的内容了,经过本文的学习后,相信大家对Java中PreparedStatement的用法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. this在java中的用法
  2. java中static的用法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java preparedstatement

上一篇:webpack+vue+node如何实现单页面

下一篇:Vue列表页渲染优化的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》