`
lovnet
  • 浏览: 6671915 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Oracle培训(三十一)—JDBC第二章知识点总结——JDBC基本应用

 
阅读更多

Oracle培训(三十一)—JDBC第二章知识点总结——JDBC基本应用

知识点预览

使用JDBC建立到数据库的连接

理解并使用JDBC相关对象

使用JDBC操作数据


使用JDBC建立到数据库的连接

1.第一个JDBC程序




2.应用JDBC的基本步骤

a)注册驱动

b)建立到数据库的连接

c)创建statement

d)执行SQL语句

e)处理results

f)关闭JDBC对象

3.Step 1--注册驱动

a)四种驱动方式

第四种:本地协议驱动



b)驱动用来连接到数据库

c)可以装载多个驱动

JDBC API使用能成功连接到指定URL(数据库)的第一个驱动

d)JDBC API使用驱动与数据库引擎建立连接

e)语法

Class.forName(driverName);

f)示例

//com.mysql.jdbc.Driver是MySQL数据库的Java驱动类名

//不同的数据库,驱动各不相同

//由数据库厂商提供,可以在数据库厂商的网站下载

Class.forName(“com.mysql.jdbc.Driver”);

g)经常使用的JDBC驱动

i.JDBC-ODBC:sun.jdbc.odbc.JdbcOdbcDriver

ii.Oracle:oracle.jdbc.driver.OracleDriver

iii.MySQL:com.mysql.jdbc.Driver

iv.Sybase:com.sybase.jdbc2.jdbc.SybDriver

v.SQLServer:com.microsoft.jdbc.sqlserver.SQLServerDriver

vi.。。。。。。

h)不是固定不变的,与数据库的版本有关

在数据库厂商的网站上下载

4.Step 2—建立到数据库的连接

a)使用驱动创建连接,连接建立后就可以访问数据

b)创建数据库连接

DriverManager类调用getConnection(urlString)方法

获得一个Connection对象

c)URL被解析用来查找数据库服务器的位置

d)当驱动收到URL(数据库)的确认响应时, DriverManager创建一个连接

e)当驱动不能匹配会返回null,并对下一个驱动进行检测

f)连接创建失败时,会抛出SQLException异常

5.创建数据库连接的过程


6.使用getConnection()方法

a)方法

getConnection(Stringurl)

b)语法

Connection con =DriverManager.getConnection(参数)

c)示例

//jdbc:mysql://computerName或IP地址 :端口/数据库名称

// 不同的数据库URL字符串格式不同

Connection con =DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test?user=root&password=root");

7.JDBC URLs语法

a)使用URL来查找数据库服务器的位置

b)常见数据库的URL

JDBC-ODBC:jdbc:odbc:ODBC名称

Oracle:jdbc:oracle:thin:@ computerName或IP地址:端口:数据库名称

MySQL: jdbc:mysql://computerName或IP地址 :端口/数据库名称

Sybase:jdbc:sybase:Tds:computerName或IP地址:端口

SQL Server:

jdbc:microsoft:sqlserver://computerName或IP地址:端口;databaseName=数据库名称

c)Connection con =DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test?user=root&password=root");

8.Step 3--创建Statement

a)Statement对象用来执行SQL语句,对数据进行操作

b)通过connection.createStatement()方法得到Statement对象

c)语法

Statement stm = connection.createStatement();

d)示例

Statement stm = null;

ResultSet rs = null;

try{

stm =connection.crateStatement();

rs = stm.executeQuery("select id, name, agefrom student");

}catch(SQLException e) {}

9.Step 4--执行SQL语句

a)通过Statement对象将SQL语句原样传送到已经建立连接的数据库并执行

b)查询数据的SQL语句执行后得到的结果集以表数据的形式存储在java.sql.ResultSet对象中,通过ResultSet对象访问查询结果

executeQuery(sqlString):执行查询相关的SQL语句,返回ResultSet对象

c)添加,删除,修改数据的SQL语句执行后返回整数,表示受到影响的行数

executeUpdate(sqlString):执行增加,修改,删除相关SQL语句或不返回任何内容的SQL语句

d)executeQuery(sqlString)

语法

Connection con = DriverManger.getConnection(urlString);

Statement stm = con.createStatement();

ResultSet rs = stm.excuteQuery(sqlString);

示例

Statement stm = con.createStatement();

ResultSet rs = stm.executeQuery(“select *from student”);

e)executeUpdate(sqlString)

语法

Connection con = DriverManger.getConnection(urlString);

Statement stm = con.createStatement();

int count = stm.excuteUpdate(sqlString);

示例

Statement stm = con.createStatement();

int count= stm.executeUpdate(“update studentset age=25 whereid=1”);


10.Step 5--处理Results

a)对数据进行添加、删除、修改等操作,SQL执行结束就已经完成

b)对数据进行查询,查询结果存放在ResultSet对象中

ResultSet对象是满足查询条件的数据行,是数据库表数据的子集

c)ResultSet




使用游标指向数据行

游标最初定位在第一行之前

boolean rs.next();

当游标指向某行数据,我们就可以从当前行取出需要的数据

d)在ResultSet对象中使用getXXX(索引或列名)方法获得数据

e)使用列名称或索引检索数据

rs.getString(2);

rs.getInt(“age”);

f)语法

While(rs.next()){

System.out.println(rs.getXXX(column);

}

g)示例

ResultSetrs = stm.executeQuery(“select* from student”);

while(rs.next()){

System.out.print("id: " +rs.getInt(1));

System.out.print("\tname: "+ rs.getString(2));

System.out.print("\tage: " +rs.getInt(3));

System.out.println("");

}

getDate(),getDouble(),getFloat(),getInt(),getLong(),getShort(),

getTime(),getString(),getBoolean(),getByte()……


11.Step 6--关闭JDBC对象

a)所有JDBC对象都需要关闭,关闭顺序应该是从小到大

ResultSet

Statement

Connection

b)示例

//关闭JDBC对象,释放资源

if(rs != null)try{ rs.close(); }catch(Exception e){}

if(stm != null)try{ stm.close(); }catch(Exception e){}

if(con != null)try{ con.close(); }catch(Exception e){}


12.应用JDBC示例


public class TestJDBC2 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
int count = 0;
String sql = "";
try {
   Class.forName("com.mysql.jdbc.Driver");
    conn =DriverManager.getConnection(
   "jdbc:mysql://localhost:3306/test?user=root&password=root");
    stmt = conn.createStatement();
    sql = "update  student set  age=25  where id=1";
    count =stmt.executeUpdate(sql);
} catch(Exception ex) {
    ex.printStackTrace();
} finally {
    try {
        if(stmt!=null){  stmt.close(); }
        if(conn!=null){  conn.close(); }
    } catch (SQLException e){  e.printStackTrace();  } 
}  }  }
 



JDBC操作数据的核心


1.JDBC操作数据的核心

a)Connection

应用程序与数据库之间的桥梁

数据库驱动程序是构建桥梁的基石和材料

DriverManager类是基石和材料的管理员

b)Statement

桥梁上的汽车,在应用程序和数据库之间运送SQL语句和执行结果

c)ResultSet

执行查询得到的数据集,由若干行和列组成的数据表,是数据库中数据表的子集,有游标

2.应用JDBC的总结

a)注册驱动

Class.forName(“com.mysql.jdbc.Driver”);

b)建立到数据库的连接

Connection con =DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test?user=root&password=root");

c)创建statement

Statement stmt = con.createStatement();

d)执行SQL语句

int count= stmt.executeUpdate(“update studentset age=25 whereid=1”);

ResultSet rs= stmt.executeQuery(“select *from student”);

e)处理results

while(rs.next()){

rs.getString(2);

}

f)关闭JDBC对象

try {

if(rs!=null){ rs.close();}

if(stmt!=null){ stmt.close();}

if(conn!=null){ conn.close();}

} catch (SQLException e) {

e.printStackTrace();

}

g)某些步骤的实现方式有多种,比如:注册驱动……


3.驱动注册的其他方式

a)注册驱动的方式有多种

b)使用类装载器(Class.forName(“类名”))

Class.forName(“com.mysql.jdbc.Driver”);

c)使用new关键字实例化驱动类

d)在属性文件中配置jdbc.drivers属性

e)省略驱动程序注册步骤

使用JDBC4.0以上版本


4.可以使用new关键字对驱动进行显式的实例化

语法

Driver drv = newDriverConstructor();

DriverManager.registerDriver(drv);

示例

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

DriverManager.registerDriver(drv);


5.在配置文件中指定驱动类名

a)语法

jdbc.drivers =driverName

b)示例

//创建一个test.properties配置文件,在属性文件中写入属性并赋值

//然后在程序中读取配置文件

jdbc.drivers=com.mysql.jdbc.Driver

适合于数据库可能会变化的情况




6.获得连接的其他方法

a)通过DriverManager类获得连接对象

b)方法

getConnection(Stringurl)

getConnection(Stringurl, String user, String passwd)

getConnection(Stringurl, java.util.properties info)

c)示例

DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,“root”,”root”);

Properties pro = newProperties();

pro.setProperty(“user”,”root”); //键是user,不是username

pro.setProperty(“password”,”root”);

Connection con =DriverManager.getConnection(

“jdbc:mysql://localhost:3306/test”, pro);

d)通过指定的驱动对象直接调用connect()方法

e)语法

Connection con =Driver.connect(urlString, properties)

f)示例

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

Connection con = null;

Properties pro = new Properties();

pro.setProperty(“user”,”root”); //键是user,不是username

pro.setProperty(“password”,”root”);

try {

con =drv.connect(“jdbc:mysql://localhost:3306/test”, pro);

}catch(SQLException e){ e.printStackTrace(); }

Statement相关接口


1.Statement相关接口

Statement:执行SQL语句,对数据库进行操作

executeQuery():查询数据

executeUpdate():添加数据,删除数据,修改数据

PreparedStatement:扩展Statement接口,对预编译的SQL语句进行处理,提高效率

CallableStatement:扩展PreparedStatement接口,执行数据库存储过程口


2.PreparedStatement对象

a)对SQL语句的编译结果在缓存中保留,提高多次执行的效率

statement每次执行sql语句,相关数据库都要先将sql语句进行编译,然后执行。而preparedstatement则将sql语句编译的结果保留,可以多次执行。

b)语法

PreparedStatementpstm = connection.prepareStatement(sqlString);

c)示例

String sql = “select* from studentwhere id=?”;

pstm = connection.prepareStatement(sql);

pstm.setInt(1,1); //setString(),setFloat()

rs =pstm.executeQuery();//executeUpdate()

……

pstm.setInt(1,2);

rs =pstm.executeQuery();


3.CallableStatement对象

a)执行数据库存储过程(数据库中定义的函数)

b)语法

CallableStatement cstm = connection.prepareCall(sqlString);

c)示例

CallabeStatement cstm = null;

try{

cstm = connection.prepareCall(“{callproc_insert_test(?,?)}”);

cstm.setString(1, “sunqi”);

cstm.setInt(2, 33);

cstm.executeUpdate();

}catch(SQLExceptione){}









4.CallableStatement对象

存储过程返回值











5.应用JDBC的基本步骤

a)注册驱动

使用类装载器(Class.forName(“类名”))

使用new关键字实例化驱动类

在属性文件中配置jdbc.drivers属性

b)建立到数据库的连接

DriverManager.getConnection(url)

DriverManager.getConnection(url,name,pass)

DriverManager.getConnection(url,properties)

驱动类connect()

c)创建statement

statement

PreparedStatement

CallableStatement

d)执行SQL语句

e)处理results

f)关闭JDBC对象


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics