时间:2021-07-01 10:21:17 帮助过:15人阅读
JDBC(Java Database Connectivity)是由数据库中间服务商提供的,用于连接数据库的Java API。一组类和接口(对接数据库)。
JNDI(Java Name Directory Interface)是为应用服务器(Tomcat)管理资源所设置的目录样式的唯一标识。(数据库、网页、文档等)
JDBC配置使用:
 // 第一步: 首先注册驱动, 驱动一般只会注册一次
try {
       Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
// 第二步:建立连接 Connect, 设置url ,用户名, 密码
   // url格式:JDBC:子协议:子名称//主机名:端口/数据库名?属性名=属性值&…
   // 注意的是url中一定不要加多余的空格,否则会出错, useSSL=false是为了解决身份验证时出现的警告的问题
   // String url = "jdbc:mysql://localhost:3306/test?" + "user=root&password=wsw011152&useUnicode=true&characterEncoding=UTF-8&useSSL=false";
      String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
      String name = "root";
      String psw = "******";
      Connection connect = null;
try {
    connect = DriverManager.getConnection(url, name, psw);
    // connect = DriverManager.getConnection(url);
    } catch (SQLException e) {
            // TODO Auto-generated catch block
      e.printStackTrace();
    }
  // 第三步: 创建一个 Statement ,一般建议使用 PreparedStatement
        // 1、执行静态SQL语句。通常通过Statement实例实现。
        // 2、执行动态SQL语句。通常通过PreparedStatement实例实现。
        // 3、执行数据库存储过程。通常通过CallableStatement实例实现。
        // String sql = "select * from user where id = ?";
        String sql = "select * from user where id = ?";
try {
     PreparedStatement ps = connect.prepareStatement(sql);
     ps.setInt(1, 1); // 设置参数
  // 第四步: 执行语句,获得一个结果集,处理获得的结果
     ResultSet result = ps.executeQuery();
      while (result.next()){            System.out.println(result.getInt("id"));
                System.out.println(result.getString("name"));
                System.out.println(result.getInt("age"));
                System.out.println(result.getString("salary"));
      }
  // 第五步: 关闭资源
     result.close();
     ps.close();
     connect.close();
    } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
  }JNDI配置使用:
在Tomcat/conf/context.xml中配置
 <Resource 
        name="jdbc/test" 
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="root" 
        password="*****" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/test"
 />在项目web.xml文件中添加配置
<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/test</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>在代码中连接、使用
DataSource ds = null;
try {
  Context initContext = new InitialContext();
  DataSource ds = (DataSource)initContext.lookup("java:comp/env/jdbc/test");
  out.print(ds);
  } catch (Exception e) {
      e.printStackTrace();
  }
connect = ds.getConnection();
....JNDI通过在Tomcat服务器的配置文件和项目的web.xml上配置参数,可以灵活、快速地获取数据库配置信息并连接。对比JDBC,当数据库参数、路径等改变时也不需要改变代码,比较灵活简单。
JNDI和JDBC的区别及使用
标签:动态 需要 generated director ini rect user tab 属性