zoukankan      html  css  js  c++  java
  • JDBC 01: 建立与数据库的连接

    示例:
    建立与数据库"web01"的连接, 并查询输出"user"表中所有的数据

    package
    com.Jasper2003.jdbc01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCDemo01 { public static void main(String[] args) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); // 使用什么驱动连接数据库 String url = "jdbc:mysql://localhost:3306/web01?useUnicode=true&characterEncoding=UTF8&useSSL=false"; String user = "root"; String password = "root"; con = DriverManager.getConnection(url,user,password); stmt = con.createStatement(); rs = stmt.executeQuery("select * from user"); while(rs.next()) { // 获取数据的第一种方式: 列数 System.out.println(rs.getInt(1)+","+rs.getString(2)+","+rs.getString(3)); // 获取数据的第二种方式: 列名 System.out.println(rs.getInt("id")+","+rs.getString("username")+","+rs.getString("password")); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(rs!=null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(stmt!=null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(con!=null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

    输出:

  • 相关阅读:
    【转】 MySQL高级知识(一)——基础
    inline元素的间距问题
    ES6对于数组的扩展
    JavaScript的垃圾回收机制
    call() apply() bind()
    防抖和节流
    Promise
    js的事件机制
    Javascript异步操作的异常处理
    JavaScript的事件执行机制及异步
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/13538963.html
Copyright © 2011-2022 走看看