JDBC连接数据库基本格式及示例代码

1、JDBC的基本格式

Oracle驱动jarojdbc-x.jar
Oracle具体驱动类oracle.jdbc.OracleDriver
Oracle连接字符串jdbc:oracle:thin:@localhost:1521:ORCL
MySQL 驱动jarmysql-connector-java-x.jar
MySQL 具体驱动类com.mysql.jdbc.Driver
MySQL 连接字符串jdbc:mysql://localhost:3306/数据库实例名
SQLServer 驱动jarsqljdbc-x.jar
SQLServer 具体驱动类com.microsoft.sqlserver.jdbc.SQLServerDriver
SQLServer 连接字符串jdbc:microsoft:sqlserver:localhost:1433;databasename=数据库实例名
  1. Class.forName(“具体驱动类”);//导入驱动包、加载具体驱动类

  2. connection = DriverManager.getConnection(…); 与数据库建立连接

  3. String sql=“SQL语句”;

  4. 通过connection,获取操作数据库的对象//.发送sql,执行(Statement\preparedStatement\callablestatement)//preparedStatement和Statement两者选一个就好了

    stmt = connection.createStatement();//Statement
    pstmt = connection.prepareStatement(sql);//preparedStatement

  5. (查询)处理结果集preparedStatement和Statement两者选一个就好了

    rs=stmt.executeUpdate(sql)//处理结果集 (查询)Statement
    rs = pstmt.executeQuery()//处理结果集 (查询)preparedStatement
    while(rs.next()){ rs.getXxx(…) ;}
    }catch(ClassNotFoundException e )
    { …}
    catch(SQLException e)
    {…
    }
    catch(Exception e)
    {…
    }

  6. 关闭资源
    finally
    {
    //打开顺序,与关闭顺序相反
    if(rs!=null)rs.close()
    if(stmt!=null) stmt.close();
    if(connection!=null)connection.close();

    }

2、JDBC API 主要功能:

三件事,具体是通过以下类/接口实现:

  1. DriverManager : 管理jdbc驱动
  2. Connection: 连接(通过DriverManager产生)
  3. Statement(PreparedStatement) :增删改查 (通过Connection产生 )
  4. CallableStatement : 调用数据库中的 存储过程/存储函数 (通过Connection产生 )
  5. Result :返回的结果集 (上面的Statement等产生 )
  6. Connection产生操作数据库的对象:
    1. Connection产生Statement对象:createStatement()
    2. Connection产生PreparedStatement对象:prepareStatement()
    3. Connection产生CallableStatement对象:prepareCall();

Statement操作数据库:

  • 增删改:executeUpdate()
  • 查询:executeQuery();
  • ResultSet:保存结果集 select * from xxx
  • next():光标下移,判断是否有下一条数据;true/false
  • previous(): true/false
  • getXxx(字段名|位置):获取具体的字段值

PreparedStatement操作数据库:
public interface PreparedStatement extends Statement
因此

  • 增删改:executeUpdate()
  • 查询:executeQuery();

此外

  • 赋值操作 setXxx();

PreparedStatement与Statement在使用时的区别:

  1. Statement:
    sql
    executeUpdate(sql)
  2. PreparedStatement:sql(可能存在占位符?)
    在创建PreparedStatement 对象时,将sql预编译 prepareStatement(sql)
    executeUpdate()
    setXxx()替换占位符?

推荐使用PreparedStatement:原因如下:

  1. 编码更加简便(避免了字符串的拼接)
    String name = “zs” ;
    int age = 23 ;
  • stmt:
    String sql =" insert into student(stuno,stuname) values(’"+name+"’, “+age+” ) " ;
    stmt.executeUpdate(sql);

  • pstmt:
    String sql =" insert into student(stuno,stuname) values(?,?) " ;
    pstmt = connection.prepareStatement(sql);//预编译SQL
    pstmt.setString(1,name);
    pstmt.setInt(2,age);

2.提高性能(因为 有预编译操作,预编译只需要执行一次)
需要重复增加100条数

  • stmt:
    String sql =" insert into student(stuno,stuname) values(’"+name+"’, “+age+” ) " ;
    for(100)
    stmt.executeUpdate(sql);

  • pstmt:
    String sql =" insert into student(stuno,stuname) values(?,?) " ;
    pstmt = connection.prepareStatement(sql);//预编译SQL
    pstmt.setString(1,name);
    pstmt.setInt(2,age);
    for( 100){
    pstmt.executeUpdate();
    }

3.安全(可以有效防止sql注入)
sql注入: 将客户输入的内容 和 开发人员的SQL语句 混为一体
stmt:存在被sql注入的风险
(例如输入 用户名:任意值 ’ or 1=1 –
密码:任意值)

分析:
select count() from login where uname=‘任意值 ’ or 1=1 --’ and upwd =‘任意值’ ;
select count(
) from login where uname='任意值 ’ or 1=1 ;
select count(*) from login ;

select count(*) from login where uname=’"+name+"’ and upwd =’"+pwd+"’

pstmt:有效防止sql注入

推荐使用pstmt

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值