JDBC基础练习.docx

上传人:b****6 文档编号:6997982 上传时间:2023-01-15 格式:DOCX 页数:24 大小:21.97KB
下载 相关 举报
JDBC基础练习.docx_第1页
第1页 / 共24页
JDBC基础练习.docx_第2页
第2页 / 共24页
JDBC基础练习.docx_第3页
第3页 / 共24页
JDBC基础练习.docx_第4页
第4页 / 共24页
JDBC基础练习.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

JDBC基础练习.docx

《JDBC基础练习.docx》由会员分享,可在线阅读,更多相关《JDBC基础练习.docx(24页珍藏版)》请在冰豆网上搜索。

JDBC基础练习.docx

JDBC基础练习

1.课程回顾

mysql加强

1)数据约束(表约束)

默认值:

default默认值

非空:

notnull

唯一:

unique

主键:

primarykey(非空+唯一)

自增长:

auto_increment

外键:

foreignkey约束两种表

2)关联查询(多表查询)

2.1交叉连接(产生笛卡尔积:

原因:

连接条件不足够)表数量-1

2.2内连接查询:

innerjoin

只有满足连接条件的数据才会显示!

2.3左【外】连接查询:

left[outer]join

左表的数据必须全部显示,用左表去匹配右表的数据,如果右表有符号条件的数据则显示符合条件的数据;如果不符合条件,则显示null。

2.4右【外】连接查询:

right[outer]join

右表的数据必须全部显示,用右表去匹配左表的数据,如果左表有符号条件的数据则显示符合条件的数据;如果不符合条件,则显示null。

2.5自连接查询

3)存储过程

--创建存储过程语法

delimeter结束符号

createprocedure名称(IN/OUT/INOUT参数名称参数类型)

begin

带逻辑的sql语句

end结束符号

--调用存储过程

CALL存储过程名称(实际参数);

今天的目标:

jdbc基础

2jdbc入门

2.1之前操作数据

1)通过mysql的客户端工具,登录数据库服务器(mysql-uroot-p密码)

2)编写sql语句

3)发送sql语句到数据库服务器执行

2.2什么是jdbc?

使用java代码(程序)发送sql语句的技术,就是jdbc技术!

2.3使用jdbc发送sql前提

登录数据库服务器(连接数据库服务器)

数据库的IP地址

端口

数据库用户名

密码

/**

*jdbc连接数据库

*@authorAPPle

*

*/

publicclassDemo1{

//连接数据库的URL

privateStringurl="jdbc:

mysql:

//localhost:

3306/day17";

//jdbc协议:

数据库子协议:

主机:

端口/连接的数据库//

privateStringuser="root";//用户名

privateStringpassword="root";//密码

/**

*第一种方法

*@throwsException

*/

@Test

publicvoidtest1()throwsException{

//1.创建驱动程序类对象

Driverdriver=newcom.mysql.jdbc.Driver();//新版本

//Driverdriver=neworg.gjt.mm.mysql.Driver();//旧版本

//设置用户名和密码

Propertiesprops=newProperties();

props.setProperty("user",user);

props.setProperty("password",password);

//2.连接数据库,返回连接对象

Connectionconn=driver.connect(url,props);

System.out.println(conn);

}

/**

*使用驱动管理器类连接数据库(注册了两次,没必要)

*@throwsException

*/

@Test

publicvoidtest2()throwsException{

Driverdriver=newcom.mysql.jdbc.Driver();

//Driverdriver2=newcom.oracle.jdbc.Driver();

//1.注册驱动程序(可以注册多个驱动程序)

DriverManager.registerDriver(driver);

//DriverManager.registerDriver(driver2);

//2.连接到具体的数据库

Connectionconn=DriverManager.getConnection(url,user,password);

System.out.println(conn);

}

/**

*(推荐使用这种方式连接数据库)

*推荐使用加载驱动程序类来注册驱动程序

*@throwsException

*/

@Test

publicvoidtest3()throwsException{

//Driverdriver=newcom.mysql.jdbc.Driver();

//通过得到字节码对象的方式加载静态代码块,从而注册驱动程序

Class.forName("com.mysql.jdbc.Driver");

//Driverdriver2=newcom.oracle.jdbc.Driver();

//1.注册驱动程序(可以注册多个驱动程序)

//DriverManager.registerDriver(driver);

//DriverManager.registerDriver(driver2);

//2.连接到具体的数据库

Connectionconn=DriverManager.getConnection(url,user,password);

System.out.println(conn);

}

}

2.4JDBC接口核心的API

java.sql.*和javax.sql.*

|-Driver接口:

表示java驱动程序接口。

所有的具体的数据库厂商要来实现此接口。

|-connect(url,properties):

连接数据库的方法。

url:

连接数据库的URL

URL语法:

jdbc协议:

数据库子协议:

//主机:

端口/数据库

user:

数据库的用户名

password:

数据库用户密码

|-DriverManager类:

驱动管理器类,用于管理所有注册的驱动程序

|-registerDriver(driver):

注册驱动类对象

|-ConnectiongetConnection(url,user,password);获取连接对象

|-Connection接口:

表示java程序和数据库的连接对象。

|-StatementcreateStatement():

创建Statement对象

|-PreparedStatementprepareStatement(Stringsql)创建PreparedStatement对象

|-CallableStatementprepareCall(Stringsql)创建CallableStatement对象

|-Statement接口:

用于执行静态的sql语句

|-intexecuteUpdate(Stringsql):

执行静态的更新sql语句(DDL,DML)

|-ResultSetexecuteQuery(Stringsql):

执行的静态的查询sql语句(DQL)

|-PreparedStatement接口:

用于执行预编译sql语句

|-intexecuteUpdate():

执行预编译的更新sql语句(DDL,DML)

|-ResultSetexecuteQuery():

执行预编译的查询sql语句(DQL)

|-CallableStatement接口:

用于执行存储过程的sql语句(callxxx)

|-ResultSetexecuteQuery():

调用存储过程的方法

|-ResultSet接口:

用于封装查询出来的数据

|-booleannext():

将光标移动到下一行

|-getXX():

获取列的值

3使用Statement执行sql语句

3.1执行DDL语句

/**

*执行DDL语句(创建表)

*/

@Test

publicvoidtest1(){

Statementstmt=null;

Connectionconn=null;

try{

//1.驱动注册程序

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接对象

conn=DriverManager.getConnection(url,user,password);

//3.创建Statement

stmt=conn.createStatement();

//4.准备sql

Stringsql="CREATETABLEstudent(idINTPRIMARYKEYAUTO_INCREMENT,NAMEVARCHAR(20),genderVARCHAR

(2))";

//5.发送sql语句,执行sql语句,得到返回结果

intcount=stmt.executeUpdate(sql);

//6.输出

System.out.println("影响了"+count+"行!

");

}catch(Exceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}finally{

//7.关闭连接(顺序:

后打开的先关闭)

if(stmt!

=null)

try{

stmt.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}

}

}

3.2执行DML语句

/**

*使用Statement执行DML语句

*@authorAPPle

*

*/

publicclassDemo2{

privateStringurl="jdbc:

mysql:

//localhost:

3306/day17";

privateStringuser="root";

privateStringpassword="root";

/**

*增加

*/

@Test

publicvoidtestInsert(){

Connectionconn=null;

Statementstmt=null;

try{

//通过工具类获取连接对象

conn=JdbcUtil.getConnection();

//3.创建Statement对象

stmt=conn.createStatement();

//4.sql语句

Stringsql="INSERTINTOstudent(NAME,gender)VALUES('李四','女')";

//5.执行sql

intcount=stmt.executeUpdate(sql);

System.out.println("影响了"+count+"行");

}catch(Exceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}finally{

//关闭资源

/*if(stmt!

=null)

try{

stmt.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}*/

JdbcUtil.close(conn,stmt);

}

}

/**

*修改

*/

@Test

publicvoidtestUpdate(){

Connectionconn=null;

Statementstmt=null;

//模拟用户输入

Stringname="陈六";

intid=3;

try{

/*//1.注册驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接对象

conn=DriverManager.getConnection(url,user,password);*/

//通过工具类获取连接对象

conn=JdbcUtil.getConnection();

//3.创建Statement对象

stmt=conn.createStatement();

//4.sql语句

Stringsql="UPDATEstudentSETNAME='"+name+"'WHEREid="+id+"";

System.out.println(sql);

//5.执行sql

intcount=stmt.executeUpdate(sql);

System.out.println("影响了"+count+"行");

}catch(Exceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}finally{

//关闭资源

/*if(stmt!

=null)

try{

stmt.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}*/

JdbcUtil.close(conn,stmt);

}

}

/**

*删除

*/

@Test

publicvoidtestDelete(){

Connectionconn=null;

Statementstmt=null;

//模拟用户输入

intid=3;

try{

/*//1.注册驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接对象

conn=DriverManager.getConnection(url,user,password);*/

//通过工具类获取连接对象

conn=JdbcUtil.getConnection();

//3.创建Statement对象

stmt=conn.createStatement();

//4.sql语句

Stringsql="DELETEFROMstudentWHEREid="+id+"";

System.out.println(sql);

//5.执行sql

intcount=stmt.executeUpdate(sql);

System.out.println("影响了"+count+"行");

}catch(Exceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}finally{

//关闭资源

/*if(stmt!

=null)

try{

stmt.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}

if(conn!

=null)

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

thrownewRuntimeException(e);

}*/

JdbcUtil.close(conn,stmt);

}

}

}

3.3执行DQL语句

/**

*使用Statement执行DQL语句(查询操作)

*@authorAPPle

*/

publicclassDemo3{

@Test

publicvoidtest1(){

Connectionconn=null;

Statementstmt=null;

try{

//获取连接

conn=JdbcUtil.getConnection();

//创建Statement

stmt=conn.createStatement();

//准备sql

Stringsql="SELECT*FROMstudent";

//执行sql

ResultSetrs=stmt.executeQuery(sql);

//移动光标

/*booleanflag=rs.next();

flag=rs.next();

flag=rs.next();

if(flag){

//取出列值

//索引

intid=rs.getInt

(1);

Stringname=rs.getString

(2);

Stringgender=rs.getString(3);

System.out.println(id+","+name+","+gender);

//列名称

intid=rs.getInt("id");

Stringname=rs.getString("name");

Stringgender=rs.getString("gender");

System.out.println(id+","+name+","+gender);

}*/

//遍历结果

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);

}

}

}

4使用PreparedStatement执行sql语句

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,"男");

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

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

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