JDBC自学入门基础.docx
- 文档编号:11889738
- 上传时间:2023-04-08
- 格式:DOCX
- 页数:19
- 大小:17.43KB
JDBC自学入门基础.docx
《JDBC自学入门基础.docx》由会员分享,可在线阅读,更多相关《JDBC自学入门基础.docx(19页珍藏版)》请在冰豆网上搜索。
JDBC自学入门基础
packagegz.itcast.c_prepared;
importgz.itcast.util.JdbcUtil;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importorg.junit.Test;
/**
*PreparedStatement執行sql語句
*@authorAPPle
*
*/
publicclassDemo1{
/**
*增加
*/
@Test
publicvoidtestInsert(){
Connectionconn=null;
PreparedStatementstmt=null;
try{
//1.获取连接
conn=JdbcUtil.getConnection();
//2.准备预编译的sql
Stringsql="INSERTINTOstudent(NAME,gender)VALUES(?
?
)";//?
表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt=conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一:
参数位置从1开始
*/
stmt.setString(1,"李四");
stmt.setString(2,"男");
//5.发送参数,执行sql
intcount=stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt);
}
}
/**
*修改
*/
@Test
publicvoidtestUpdate(){
Connectionconn=null;
PreparedStatementstmt=null;
try{
//1.获取连接
conn=JdbcUtil.getConnection();
//2.准备预编译的sql
Stringsql="UPDATEstudentSETNAME=?
WHEREid=?
";//?
表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt=conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一:
参数位置从1开始
*/
stmt.setString(1,"王五");
stmt.setInt(2,9);
//5.发送参数,执行sql
intcount=stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt);
}
}
/**
*删除
*/
@Test
publicvoidtestDelete(){
Connectionconn=null;
PreparedStatementstmt=null;
try{
//1.获取连接
conn=JdbcUtil.getConnection();
//2.准备预编译的sql
Stringsql="DELETEFROMstudentWHEREid=?
";//?
表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt=conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一:
参数位置从1开始
*/
stmt.setInt(1,9);
//5.发送参数,执行sql
intcount=stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt);
}
}
/**
*查询
*/
@Test
publicvoidtestQuery(){
Connectionconn=null;
PreparedStatementstmt=null;
ResultSetrs=null;
try{
//1.获取连接
conn=JdbcUtil.getConnection();
//2.准备预编译的sql
Stringsql="SELECT*FROMstudent";
//3.预编译
stmt=conn.prepareStatement(sql);
//4.执行sql
rs=stmt.executeQuery();
//5.遍历rs
while(rs.next()){
intid=rs.getInt("id");
Stringname=rs.getString("name");
Stringgender=rs.getString("gender");
System.out.println(id+","+name+","+gender);
}
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn,stmt,rs);
}
}
}
packagegz.itcast.c_prepared;
importgz.itcast.util.JdbcUtil;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.Statement;
importorg.junit.Test;
/**
*模拟用户登录效果
*@authorAPPle
*
*/
publicclassDemo2{
//模拟用户输入
//privateStringname="ericdfdfdfddfd'OR1=1--";
privateStringname="eric";
//privateStringpassword="123456dfdfddfdf";
privateStringpassword="123456";
/**
*Statment存在sql被注入的风险
*/
@Test
publicvoidtestByStatement(){
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
//获取连接
conn=JdbcUtil.getConnection();
//创建Statment
stmt=conn.createStatement();
//准备sql
Stringsql="SELECT*FROMusersWHERENAME='"+name+"'ANDPASSWORD='"+password+"'";
//执行sql
rs=stmt.executeQuery(sql);
if(rs.next()){
//登录成功
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt,rs);
}
}
/**
*PreparedStatement可以有效地防止sql被注入
*/
@Test
publicvoidtestByPreparedStatement(){
Connectionconn=null;
PreparedStatementstmt=null;
ResultSetrs=null;
try{
//获取连接
conn=JdbcUtil.getConnection();
Stringsql="SELECT*FROMusersWHERENAME=?
ANDPASSWORD=?
";
//预编译
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(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt,rs);
}
}
}
packagegz.itcast.d_callable;
importgz.itcast.util.JdbcUtil;
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.ResultSet;
importorg.junit.Test;
/**
*使用CablleStatement调用存储过程
*@authorAPPle
*
*/
publicclassDemo1{
/**
*调用带有输入参数的存储过程
*CALLpro_findById(4);
*/
@Test
publicvoidtest1(){
Connectionconn=null;
CallableStatementstmt=null;
ResultSetrs=null;
try{
//获取连接
conn=JdbcUtil.getConnection();
//准备sql
Stringsql="CALLpro_findById(?
)";//可以执行预编译的sql
//预编译
stmt=conn.prepareCall(sql);
//设置输入参数
stmt.setInt(1,6);
//发送参数
rs=stmt.executeQuery();//注意:
所有调用存储过程的sql语句都是使用executeQuery方法执行!
!
!
//遍历结果
while(rs.next()){
intid=rs.getInt("id");
Stringname=rs.getString("name");
Stringgender=rs.getString("gender");
System.out.println(id+","+name+","+gender);
}
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt,rs);
}
}
/**
*执行带有输出参数的存储过程
*CALLpro_findById2(5,@NAME);
*/
@Test
publicvoidtest2(){
Connectionconn=null;
CallableStatementstmt=null;
ResultSetrs=null;
try{
//获取连接
conn=JdbcUtil.getConnection();
//准备sql
Stringsql="CALLpro_findById2(?
?
)";//第一个?
是输入参数,第二个?
是输出参数
//预编译
stmt=conn.prepareCall(sql);
//设置输入参数
stmt.setInt(1,6);
//设置输出参数(注册输出参数)
/**
*参数一:
参数位置
*参数二:
存储过程中的输出参数的jdbc类型VARCHAR(20)
*/
stmt.registerOutParameter(2,java.sql.Types.VARCHAR);
//发送参数,执行
stmt.executeQuery();//结果不是返回到结果集中,而是返回到输出参数中
//得到输出参数的值
/**
*索引值:
预编译sql中的输出参数的位置
*/
Stringresult=stmt.getString
(2);//getXX方法专门用于获取存储过程中的输出参数
System.out.println(result);
}catch(Exceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
JdbcUtil.close(conn,stmt,rs);
}
}
}
packagegz.itcast.util;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
/**
*jdbc工具类
*@authorAPPle
*
*/
publicclassJdbcUtil{
privatestaticStringurl=null;
privatestaticStringuser=null;
privatestaticStringpassword=null;
privatestaticStringdriverClass=null;
/**
*静态代码块中(只加载一次)
*/
static{
try{
//读取db.properties文件
Propertiesprops=newProperties();
/**
*.代表java命令运行的目录
*在java项目下,.java命令的运行目录从项目的根目录开始
*在web项目下,.java命令的而运行目录从tomcat/bin目录开始
*所以不能使用点.
*/
//FileInputStreamin=newFileInputStream("./src/db.properties");
/**
*使用类路径的读取方式
*/:
斜杠表示classpath的根目录
*在java项目下,classpath的根目录从bin目录开始
*在web项目下,classpath的根目录从WEB-INF/classes目录开始
*/
InputStreamin=JdbcUtil.class.getResourceAsStream("/db.properties");
//加载文件
props.load(in);
//读取信息
url=props.getProperty("url");
user=props.getProperty("user");
password=props.getProperty("password");
driverClass=props.getProperty("driverClass");
//注册驱动程序
Class.forName(driverClass);
}catch(Exceptione){
e.printStackTrace();
System.out.println("驱程程序注册出错");
}
}
/**
*抽取获取连接对象的方法
*/
publicstaticConnectiongetConnection(){
try{
Connectionconn=DriverManager.getConnection(url,user,password);
returnconn;
}catch(SQLExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
/**
*释放资源的方法
*/
publicstaticvoidclose(Connectionconn,Statementstmt){
if(stmt!
=null){
try{
stmt.close();
}catch(SQLExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
if(conn!
=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
publicstaticvoidclose(Connectionconn,Statementstmt,ResultSetrs){
if(rs!
=null)
try{
rs.close();
}catch(SQLExceptione1){
e1.printStackTrace();
thrownewRuntimeException(e1);
}
if(stmt!
=null){
try{
stmt.close();
}catch(SQLExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
if(conn!
=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JDBC 自学 入门 基础