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
    }
    }
    

      

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

  • 相关阅读:
    小透明学弟的华为上岸之路
    手把手体验远程开发,确实爽
    老弟做了个网盘,炸了!
    聊聊我在腾讯和字节工作感受
    2021,编程语言如何选择?
    优化了破网站的搜索功能
    15 道超经典大厂 Java 面试题!重中之重
    我两年的坚持,值了!
    聊聊百度搜索背后的故事
    struts2的配置步骤
  • 原文地址:https://www.cnblogs.com/tk55/p/6064751.html
Copyright © 2011-2022 走看看