ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:17.43KB ,
资源ID:11889738      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11889738.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JDBC自学入门基础.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JDBC自学入门基础.docx

1、JDBC自学入门基础package gz.itcast.c_prepared;import gz.itcast.util.JdbcUtil;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import org.junit.Test;/* * PreparedStatement執行sql語句 * author APPle * */public class Demo1 /* * 增加 */ Test public void testInsert() Connection c

2、onn = null; PreparedStatement stmt = null; try /1.获取连接 conn = JdbcUtil.getConnection(); /2.准备预编译的sql String sql = INSERT INTO student(NAME,gender) VALUES(?,?); /?表示一个参数的占位符 /3.执行预编译sql语句(检查语法) stmt = conn.prepareStatement(sql); /4.设置参数值 /* * 参数一: 参数位置 从1开始 */ stmt.setString(1, 李四); stmt.setString(2,

3、 男); /5.发送参数,执行sql int count = stmt.executeUpdate(); System.out.println(影响了+count+行); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally JdbcUtil.close(conn, stmt); /* * 修改 */ Test public void testUpdate() Connection conn = null; PreparedStatement stmt = null; try /1.获取连

4、接 conn = JdbcUtil.getConnection(); /2.准备预编译的sql String sql = UPDATE student SET NAME=? WHERE id=?; /?表示一个参数的占位符 /3.执行预编译sql语句(检查语法) stmt = conn.prepareStatement(sql); /4.设置参数值 /* * 参数一: 参数位置 从1开始 */ stmt.setString(1, 王五); stmt.setInt(2, 9); /5.发送参数,执行sql int count = stmt.executeUpdate(); System.out.

5、println(影响了+count+行); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally JdbcUtil.close(conn, stmt); /* * 删除 */ Test public void testDelete() Connection conn = null; PreparedStatement stmt = null; try /1.获取连接 conn = JdbcUtil.getConnection(); /2.准备预编译的sql String sql = DEL

6、ETE FROM student WHERE id=?; /?表示一个参数的占位符 /3.执行预编译sql语句(检查语法) stmt = conn.prepareStatement(sql); /4.设置参数值 /* * 参数一: 参数位置 从1开始 */ stmt.setInt(1, 9); /5.发送参数,执行sql int count = stmt.executeUpdate(); System.out.println(影响了+count+行); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e);

7、 finally JdbcUtil.close(conn, stmt); /* * 查询 */ Test public void testQuery() Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try /1.获取连接 conn = JdbcUtil.getConnection(); /2.准备预编译的sql String sql = SELECT * FROM student; /3.预编译 stmt = conn.prepareStatement(sql); /4.执行sql rs

8、 = stmt.executeQuery(); /5.遍历rs while(rs.next() int id = rs.getInt(id); String name = rs.getString(name); String gender = rs.getString(gender); System.out.println(id+,+name+,+gender); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally /关闭资源 JdbcUtil.close(conn,stmt,rs);

9、package gz.itcast.c_prepared;import gz.itcast.util.JdbcUtil;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import org.junit.Test;/* * 模拟用户登录效果 * author APPle * */public class Demo2 /模拟用户输入 /private String name = ericdfdfdfddfd OR 1=1

10、- ; private String name = eric; /private String password = 123456dfdfddfdf; private String password = 123456; /* * Statment存在sql被注入的风险 */ Test public void testByStatement() Connection conn = null; Statement stmt = null; ResultSet rs = null; try /获取连接 conn = JdbcUtil.getConnection(); /创建Statment stmt

11、 = conn.createStatement(); /准备sql String sql = SELECT * FROM users WHERE NAME=+name+ AND PASSWORD=+password+; /执行sql rs = stmt.executeQuery(sql); if(rs.next() /登录成功 System.out.println(登录成功); else System.out.println(登录失败); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finall

12、y JdbcUtil.close(conn, stmt ,rs); /* * PreparedStatement可以有效地防止sql被注入 */ Test public void testByPreparedStatement() Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try /获取连接 conn = JdbcUtil.getConnection(); String sql = SELECT * FROM users WHERE NAME=? AND PASSWORD=?; /预编

13、译 stmt = conn.prepareStatement(sql); /设置参数 stmt.setString(1, name); stmt.setString(2, password); /执行sql rs = stmt.executeQuery(); if(rs.next() /登录成功 System.out.println(登录成功); else System.out.println(登录失败); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally JdbcUtil.close

14、(conn, stmt ,rs); package gz.itcast.d_callable;import gz.itcast.util.JdbcUtil;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.ResultSet;import org.junit.Test;/* * 使用CablleStatement调用存储过程 * author APPle * */public class Demo1 /* * 调用带有输入参数的存储过程 * CALL pro_findById(4); */

15、Test public void test1() Connection conn = null; CallableStatement stmt = null; ResultSet rs = null; try /获取连接 conn = JdbcUtil.getConnection(); /准备sql String sql = CALL pro_findById(?); /可以执行预编译的sql /预编译 stmt = conn.prepareCall(sql); /设置输入参数 stmt.setInt(1, 6); /发送参数 rs = stmt.executeQuery(); /注意: 所有

16、调用存储过程的sql语句都是使用executeQuery方法执行! /遍历结果 while(rs.next() int id = rs.getInt(id); String name = rs.getString(name); String gender = rs.getString(gender); System.out.println(id+,+name+,+gender); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally JdbcUtil.close(conn, stmt ,r

17、s); /* * 执行带有输出参数的存储过程 * CALL pro_findById2(5,NAME); */ Test public void test2() Connection conn = null; CallableStatement stmt = null; ResultSet rs = null; try /获取连接 conn = JdbcUtil.getConnection(); /准备sql String sql = CALL pro_findById2(?,?); /第一个?是输入参数,第二个?是输出参数 /预编译 stmt = conn.prepareCall(sql);

18、 /设置输入参数 stmt.setInt(1, 6); /设置输出参数(注册输出参数) /* * 参数一: 参数位置 * 参数二: 存储过程中的输出参数的jdbc类型 VARCHAR(20) */ stmt.registerOutParameter(2, java.sql.Types.VARCHAR); /发送参数,执行 stmt.executeQuery(); /结果不是返回到结果集中,而是返回到输出参数中 /得到输出参数的值 /* * 索引值: 预编译sql中的输出参数的位置 */ String result = stmt.getString(2); /getXX方法专门用于获取存储过程中

19、的输出参数 System.out.println(result); catch (Exception e) e.printStackTrace(); throw new RuntimeException(e); finally JdbcUtil.close(conn, stmt ,rs); package gz.itcast.util;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLEx

20、ception;import java.sql.Statement;import java.util.Properties;/* * jdbc工具类 * author APPle * */public class JdbcUtil private static String url = null; private static String user = null; private static String password = null; private static String driverClass = null; /* * 静态代码块中(只加载一次) */ static try /

21、读取db.properties文件 Properties props = new Properties(); /* * . 代表java命令运行的目录 * 在java项目下,. java命令的运行目录从项目的根目录开始 * 在web项目下, . java命令的而运行目录从tomcat/bin目录开始 * 所以不能使用点. */ /FileInputStream in = new FileInputStream(./src/db.properties); /* * 使用类路径的读取方式 * / : 斜杠表示classpath的根目录 * 在java项目下,classpath的根目录从bin目录开

22、始 * 在web项目下,classpath的根目录从WEB-INF/classes目录开始 */ InputStream in = JdbcUtil.class.getResourceAsStream(/db.properties); /加载文件 props.load(in); /读取信息 url = props.getProperty(url); user = props.getProperty(user); password = props.getProperty(password); driverClass = props.getProperty(driverClass); /注册驱动程

23、序 Class.forName(driverClass); catch (Exception e) e.printStackTrace(); System.out.println(驱程程序注册出错); /* * 抽取获取连接对象的方法 */ public static Connection getConnection() try Connection conn = DriverManager.getConnection(url, user, password); return conn; catch (SQLException e) e.printStackTrace(); throw new

24、 RuntimeException(e); /* * 释放资源的方法 */ public static void close(Connection conn,Statement stmt) if(stmt!=null) try stmt.close(); catch (SQLException e) e.printStackTrace(); throw new RuntimeException(e); if(conn!=null) try conn.close(); catch (SQLException e) e.printStackTrace(); throw new RuntimeExc

25、eption(e); public static void close(Connection conn,Statement stmt,ResultSet rs) if(rs!=null) try rs.close(); catch (SQLException e1) e1.printStackTrace(); throw new RuntimeException(e1); if(stmt!=null) try stmt.close(); catch (SQLException e) e.printStackTrace(); throw new RuntimeException(e); if(conn!=null) try conn.close(); catch (SQLException e) e.printStackTrace(); throw new RuntimeException(e);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1