標籤:string 輸出 資料庫名 while sys images long body tor
首先要匯入jar包。
剩下的基本就是模版式的代碼了:
public class main {// JDBC 驅動名及資料庫 URLstatic final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/test";//連接埠號碼,資料庫名 // 資料庫的使用者名稱與密碼,需要根據自己的設定static final String USER = "root";static final String PASS = "";public static void main(String args[]){Connection conn = null;Statement stmt = null;try{// 註冊 JDBC 驅動Class.forName("com.mysql.jdbc.Driver");// 開啟連結System.out.println("串連資料庫...");conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);// 執行查詢System.out.println(" 執行個體化Statement對...");stmt = (Statement) conn.createStatement();String sql;sql = "INSERT INTO students VALUES(\"li\",\"1234\")";//向資料庫中插入資料 stmt.executeUpdate(sql);sql = "SELECT unmber, name FROM students";ResultSet rs = stmt.executeQuery(sql);// 展開結果集資料庫while(rs.next()){// 通過欄位檢索String name = rs.getString("name");String id = rs.getString("unmber");// 輸出資料System.out.print("ID: " + id);System.out.print(", 名稱: " + name);System.out.print("\n");}// 完成後關閉rs.close();stmt.close();conn.close();}catch(SQLException se){// 處理 JDBC 錯誤se.printStackTrace();}catch(Exception e){// 處理 Class.forName 錯誤e.printStackTrace();}finally{// 關閉資源try{if(stmt!=null) stmt.close();}catch(SQLException se2){}// 什麼都不做try{if(conn!=null) conn.close();}catch(SQLException se){se.printStackTrace();}}System.out.println("Goodbye!");}}
Servlet通過java調用資料庫
基本上與java直接調用一模一樣。
但是有一點很重要!!!!!!!!!!!
千萬不要把jar包匯入到web項目下,會報ClassNotFound異常。
把jar包刪了,放到tomcat的lib下就行。
public class Hello extends HttpServlet { private static final long serialVersionUID = 1L;// JDBC 驅動名及資料庫 URLstatic final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/test";// 資料庫的使用者名稱與密碼,需要根據自己的設定static final String USER = "root";static final String PASS = ""; /** * @see HttpServlet#HttpServlet() */ public Hello() { super(); // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Connection conn = null;Statement stmt = null;// 設定響應內容類型response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String title = "Servlet Mysql";String docType = "<!DOCTYPE html>\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n");try{// 註冊 JDBC 磁碟機Class.forName("com.mysql.jdbc.Driver");// 開啟一個串連conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);// 執行 SQL 查詢stmt = (Statement) conn.createStatement();String sql;sql = "SELECT unmber, name FROM students";ResultSet rs = stmt.executeQuery(sql);// 展開結果集資料庫while(rs.next()){// 通過欄位檢索String name = rs.getString("name");String id = rs.getString("unmber");// 輸出資料out.println("ID: " + id);out.println(",名稱: " + name);out.println("<br />");}out.println("</body></html>");// 完成後關閉rs.close();stmt.close();conn.close();} catch(SQLException se) {// 處理 JDBC 錯誤se.printStackTrace();} catch(Exception e) {// 處理 Class.forName 錯誤e.printStackTrace();}finally{// 最後是用於關閉資源的塊try{if(stmt!=null)stmt.close();}catch(SQLException se2){}try{if(conn!=null)conn.close();}catch(SQLException se){se.printStackTrace();}} }/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}}
Java Servlet調用資料庫複習