zoukankan      html  css  js  c++  java
  • java输出MYSQL数据库里面的数据最简单的实例

    import java.sql.*;
    public class JDBCExample {
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
       static final String DB_URL = "jdbc:mysql://localhost:3306/gbooks";
    
       static final String USER = "root";
       static final String PASS = "";
    
       public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
    
       try{
          Class.forName("com.mysql.jdbc.Driver");
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "select * from admin";
          ResultSet rs = stmt.executeQuery(sql);
          while(rs.next()){
             System.out.print("ID: " + rs.getInt("id"));
             System.out.print(", webtitle  : " + rs.getString("webtitle"));
             System.out.print(", adminhttp : " + rs.getString("adminhttp"));
             System.out.print(", adminname : " + rs.getString("adminname"));
             System.out.print(", adminpass : " + rs.getString("adminpass"));
             System.out.print(", adminmail : " + rs.getString("adminmail"));
             System.out.print(", webadvice : " + rs.getString("adminmail"));
             System.out.println(", webadvice : " + rs.getString("webadvice"));
          }
          rs.close();
          stmt.close();
          conn.close();
       }catch(SQLException se){
          se.printStackTrace();
       }catch(Exception e){
          e.printStackTrace();
       }finally{
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
    }
    }
    

      

    ****************************************************************************************【来自我另一博文】

  • 相关阅读:
    【oracle ocp知识点一】
    poj 3280 区间dp
    TextView划线 android
    Apple Watch 集成环信SDK
    Android中使用HttpClient实现HTTP通信效果
    js 数组操作大集合
    5.5二叉树的遍历
    oracle学习之路(二)------数组类型/记录类型的使用
    推荐系统个人理解(实践部分)
    openstack之虚拟机创建流程分析
  • 原文地址:https://www.cnblogs.com/tk55/p/6064751.html
Copyright © 2011-2022 走看看