jdbc连接数据库步骤(mysql、oracle、sqlserver2008)

3 篇文章 0 订阅
3 篇文章 0 订阅

•创建一个以JDBC连接数据库的程序,包含7个步骤:

1、加载JDBC驱动程序: 
在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 
这通过java.lang.Class类的静态方法forName(String className)实现。 
2、提供JDBC连接的URL

数据库 Class.forName url
sqlserver2008 com.microsoft.jdbc.sqlserver.SQLServerDriver jdbc:sqlserver://localhost:1433;DataBase=数据库名称
mysql com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/数据库名称
oracle oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:’数据库称’

3、创建数据库的连接 
•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象, 
该对象就代表一个数据库的连接。 
•使用DriverManager的getConnectin(String url , String username , 
String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和 
密码来获得。

- 以oracle为例

public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");//jdbc驱动
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:news", "scott", "123456");
                    //url scott为用户名 123456为密码

            System.out.println("成功");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return connection;

    }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4、创建一个Statement

1、执行静态SQL语句。通常通过Statement实例实现。 
2、执行动态SQL语句。通常通过PreparedStatement实例实现。 
3、执行数据库存储过程。通常通过CallableStatement实例实现。

 具体的实现方式:   
        Statement stmt = con.createStatement() ;   
       PreparedStatement pstmt = con.prepareStatement(sql) ;   
       CallableStatement cstmt =con.prepareCall("{CALL demoSp(? , ?)}") ;  
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

5、执行SQL语句 
Statement接口提供了三种执行SQL语句的方法: 
executeQuery 、executeUpdate 和execute 
1、ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 ,返回结果集(ResultSet)对象。 
2、int executeUpdate(String sqlString):用于执行INSERT、UPDATE或 DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等 
3、execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的 语句。 
6、处理结果 
• ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些 行中数据的访问。 
• 使用结果集(ResultSet)对象的访问方法获取数据:

while(rs.next()){   
         String name = rs.getString("name") ;  //数据库中字段 
    String pass = rs.getString(1) ; // 此方法比较高效
    }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

7、关闭JDBC对象

    try {
            if (rs != null)
                rs.close();
            if (pst != null)
                pst.close();
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

三大数据库jar包下载地址—> 数据库jar


本文转载:http://blog.csdn.net/qq_33647275/article/details/52613613

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值