使用JDBC连接数据库(MySQL)的源代码
生活随笔
收集整理的這篇文章主要介紹了
使用JDBC连接数据库(MySQL)的源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- JDBC 訪問數據庫的步驟
- 使用 JDBC 訪問數據庫的演示代碼
- 使用 PreparedStatement 對象
- 查詢
- 插入
- 更新
- 刪除
- 使用 Statement 對象
- 查詢
- 刪除
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)的源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星推出 85 英寸户外电视,价格高达
- 下一篇: MySQL 数据库命令之 mysqlsh