zoukankan      html  css  js  c++  java
  • Java学习

    学习内容

    读出数据库的内容

    代码实例:

    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    //需要导入的包
    @SuppressWarnings("unused")
    public class test {

    public static void main(String[] args) {
    try {
    String driver="com.mysql.cj.jdbc.Driver";//定义驱动程序名为driver内容
    String url="jdbc:mysql://localhost:3306/db? useSSL=false&serverTimezone=GMT";//定义url,db为数据库名称
    String user="root";//定义想要连接到的用户
    String pass="020714";//用户密码
    String querySql="select * from Yingjie";//定义使用的SQL语句
    Class.forName(driver);//加载驱动程序
    Connection conn=DriverManager.getConnection(url,user,pass);//建立与Mysql数据库的连接
    Statement stmt=conn.createStatement();//构造一个Statement对象执行sql语句
    ResultSet rs=stmt.executeQuery(querySql);//执行sql并返回结果集
    while(rs.next()) {//遍历结果集
    System.out.println("id:"+rs.getInt("id")+" name:"+rs.getString("name")+" sex:"+rs.getString("sex"));
    }//
    if(rs!=null) {//关闭结果集
    try {
    rs.close();
    }catch(SQLException e) {
    e.printStackTrace();
    }
    }//
    if(stmt!=null) {//关闭statement对象
    try {
    stmt.close();
    }catch(SQLException e) {
    e.printStackTrace();
    }
    }//
    if(conn!=null) {//关闭连接
    try {
    conn.close();
    }catch(SQLException e) {
    e.printStackTrace();
    }
    }//
    }catch(Exception e) {
    e.printStackTrace();
    }

    }

    }

  • 相关阅读:
    启动 Appium 自带模拟器
    Android的一些常用命令提示符(cmd)指令
    Eclipse中没有andriod问题解决方法
    selenium键盘操作
    鼠标事件
    appium运行from appium import webdriver 提示most recent call last
    appium for windows 环境搭建
    python+Eclipse+pydev环境搭建
    nmon和nmon analyser的下载和使用
    Linux下使用NMON监控、分析系统性能
  • 原文地址:https://www.cnblogs.com/wrljzb/p/14162281.html
Copyright © 2011-2022 走看看