gistfile1.txt package cn.xuzihui.db;import java.sql.*;public class DBUtil { //URL中的Book为数据库的名称private static final String URL = "jdbc:mysql://localhost:3306/Book";private static final String USER = "root";private static fi
package cn.xuzihui.db; import java.sql.*; public class DBUtil { //URL中的Book为数据库的名称 private static final String URL = "jdbc:mysql://localhost:3306/Book"; private static final String USER = "root"; private static final String PASSWORD = "123456"; public static void main(String[] args) { Connection conn = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, USER, PASSWORD); st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from bookinfo"); while(rs.next()) { String bookname = rs.getString("book_name"); String bookauthor = rs.getString("book_author"); String bookabstract = rs.getString("book_abstract"); System.out.println("Name:"+bookname+"
"); System.out.println("Author:"+bookauthor+"
"); } rs.close(); st.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); // 处理 JDBC 错误 } catch (Exception e) { e.printStackTrace(); // 处理 Class.forName 错误 } finally { try { // 关闭资源 if(st!=null) st.close(); } catch (SQLException se2) {} try { if(conn!=null) conn.close(); } catch (SQLException se3) { se3.printStackTrace(); } } System.out.println("GoodBye!"); } }