日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

使用JDBC连接数据库(MySQL)的源代码

發布時間:2023/12/3 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用JDBC连接数据库(MySQL)的源代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • JDBC 訪問數據庫的步驟
  • 使用 JDBC 訪問數據庫的演示代碼
    • 使用 PreparedStatement 對象
      • 查詢
      • 插入
      • 更新
      • 刪除
    • 使用 Statement 對象
      • 查詢
      • 刪除

JDBC 訪問數據庫的步驟

  • 將 jdbc 驅勱程序相關的 jar 包 copy 到 WEB-INF/lib 下
  • 在 servlet 代碼當中,使用 jdbc 訪問數據庫,要注意如何處理異常
  • 配置錯誤處理頁面
  • 使用 JDBC 訪問數據庫的演示代碼

    使用 PreparedStatement 對象

    查詢

    public class LoadEmpServlet extends HttpServlet { public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {long id = Long.parseLong(request.getParameter("id"));// 訪問數據庫 Connection conn = null; try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jd1109db2", "root","root");PreparedStatement prep = conn.prepareStatement("select * from t_emp where id = ?");prep.setLong(1, id);ResultSet rst = prep.executeQuery(); response.setContentType("text/ htm l;charset=utf-8"); PrintWriter out = response.getWriter();if (rst.next()) {String name = rst.getString("name");double salary = rst.getDouble("salary");int age = rst.getInt("age");out.println("<form action='modify?id=" + id + "' method='post'>");out.println("id:" + id + "<br/>");out.println("姓名:<input name='name' value='" +name + "'/><br/>"); out.println("薪水:<input name='salary' value='" +salary + "'/><br/>"); out.println("年齡:<input name='age' value='" + age + "'/><br/>");out.println("<input type='submit' " + "value='確認'/>");out.println( "</form>");out.close();} } catch (Exception e) {e.printStackTrace();throw new ServletException(e); } finally {if(conn!=null) { try {conn.close();} catch (SQLException e) {e.printStackTrace(); }} }} }

    插入

    public class AddEmpServlet extends HttpServlet {public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { //這行代碼要放在 getParameter()執行之前request.setCharacterEncoding("utf-8");String name = request.getParameter("name");double salary = Double.parseDouble(request.getParameter("salary"));int age = Integer.parseInt(request.getParameter("age")); System.out.println("name:" + name);System.out.println("salary:" + salary); System.out.println("age:" + age);//訪問數據庫 Connection conn = null; try {Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jd1109db2", "root","root");PreparedStatement prep = conn.prepareStatement("insert into t_emp(name,salary,age) values(?,?,?)");prep.setString(1, name);prep.setDouble(2, salary); prep.setInt(3, age); prep.executeUpdate();response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter(); out.println( "添加雇員成功");out.close();} catch (Exception e) {e.printStackTrace();throw new ServletException(e); } finally {if(conn!=null) { try {conn.close();} catch (SQLException e) {e.printStackTrace();} }}} }

    更新

    public class ModifyEmpServlet extends HttpServlet { public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { request.setCharacterEncoding("utf-8"); long id = Long.parseLong(request.getParameter("id")); String name = request.getParameter("name"); double salary = Double.parseDouble(request.getParameter("salary"));int age = Integer.parseInt(request.getParameter("age"));// 訪問數據庫Connection conn = null; try {Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/jd1109db2","root","root");PreparedStatement prep = conn.prepareStatement("update t_emp " +"set name=?,salary=?,age=? " + "where id=?");prep.setString(1, name);prep.setDouble(2, salary); prep.setLong(3, age); prep.setLong(4, id); prep.executeUpdate();response.sendRedirect("list"); } catch (Exception e) {e.printStackTrace();throw new ServletException(e); } finally {if(conn!=nul l) { try {conn.close();} catch (SQLException e) { e.printStackTrace();}} }}}

    刪除

    public class DelEmpServlet extends HttpServlet {public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {long id = Long.parseLong(request.getParameter("id"));Connection conn = null;// 訪問數據庫try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager .getConnection("jdbc:mysql://localhost:3306/jd1109db2","root","root");PreparedStatement prep = conn.prepareStatement("delete from t_emp where id = ?"); prep.setLong(1, id);prep.executeUpdate();//重定向 response.sendRedirect("list");} catch (Exception e) { //step1 先記錄日志e.printStackTrace(); //step2 拋出throw new ServletException(e); } finally {if(conn!=nul l) { try {conn.close();} catch (SQLException e) { e.printStackTrace();}} }}}

    使用 Statement 對象

    查詢

    利用 JDBC 訪問數據庫 , 顯示所有雇員信息

    public class ListEmpServlet extends HttpServlet{public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {//訪問數據庫 Connection conn = null; try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jd1109db2","root","root"); Statement stat = conn.createStatement();ResultSet rst = stat.executeQuery("select * from t_emp");//使用查詢得到的結果,生成一個表格 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<table border='1' " + "width='60%' " +"cellpadding='0' " +"cellspacing='0'>");out.println("<tr>" +"<td>id</td>" + "<td>姓名</td>" +"<td>薪水</td>" + "<td>年齡</td></tr>");while (rst.next()) {long id = rst.getLong("id");String name = rst.getString("name");double salary = rst.getDouble("salary");int age = rst.getInt("age");out.println("<tr><td>" + id + "</td><td> " + name + "</td><td>" + salary + "</td><td> " + age + "</td></tr>");}out.println( "</table>");out.close();} catch (Exception e) { e.printStackTrace();throw new ServletException(e);} finally { if(conn!=nul l) {try {conn.close();} catch (SQLException e) {e.printStackTrace(); }} }}}

    刪除

    待續

    總結

    以上是生活随笔為你收集整理的使用JDBC连接数据库(MySQL)的源代码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。