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

      

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

  • 相关阅读:
    PHP调试的时候出现了警告:
    快报滚动
    js foreach、map函数
    箭头函数和普通函数的区别
    flex布局
    react+propTypes
    手机尺寸
    less的使用
    发现是在IE6-IE9下,下列元素table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset的innerHTML属性是只读的
    div+css 组织结构
  • 原文地址:https://www.cnblogs.com/tk55/p/6064751.html
Copyright © 2011-2022 走看看