การเข ยนโปรแกรมต ดต_อฐานข_อม_ล

216 views
184 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
216
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

การเข ยนโปรแกรมต ดต_อฐานข_อม_ล

  1. 1. 1 .JSP Eclipse 1. Eclipse 2. AppServ 2.5.10 Java eclipse MySQL - AppServ 2.5.10 Appserv
  2. 2. 2 Next > License Agreement AppServ 2.5.10 I Agree
  3. 3. 3 Next > Select Components Next > HTTPServer
  4. 4. 4 ServerName : localhost Email: E-mail Port: 80 Next > MySQL
  5. 5. 5 Install Windows Security Alert Allow access
  6. 6. 6 Finish appserv web server
  7. 7. 7 browser localhost 3. Navicat MySQL localhost http://www.navicat.com/download/d ownload.html
  8. 8. 8 Next I accept the agreement Next (
  9. 9. 9 Next Next
  10. 10. 10 Next > Install
  11. 11. 11 Finish Navicat 4. Java Eclipse (MySQL) 4.1 project 1project project BookMark 4.2 package 2package Package comment Package controller - Package comment java class class connect package comment;
  12. 12. 12 importjava.sql.DriverManager; importjava.sql.SQLException; importjava.sql.Statement; importjava.sql.Connection; importjava.util.logging.Level; importjava.util.logging.Logger; public class connect { public Connection conn =null; public Statement stmt = null; public Statement getStatement(){ returnstmt;} public void createConnection() throws SQLException{ try{ Class.forName("com.mysql.jdbc.Driver");
  13. 13. 13 conn = DriverManager.getConnection("jdbc:mysql://localhost/b ook","root","admin"); stmt= conn.createStatement(); System.out.println("Connect success."); }catch(ClassNotFoundException ex) { Logger.getLogger(connect.class.getName()).l og(Level.SEVERE, null, ex); System.out.println("Connect Fail."); } } public void closeConnection(){ try{ conn.close(); }catch(SQLException ex){ Logger.getLogger(connect.class.getName()).l
  14. 14. 14 og(Level.SEVERE, null, ex); } } } - package controller servlet loginservlet import classconnect loginservlet packagecom.controller; importjava.io.IOException; importjava.sql.SQLException; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importcomment.connect; Servlet implementation class LoginServlet
  15. 15. 15 @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = L; Default constructor. publicLoginServlet() { TODO Auto-generated constructor stub } @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { TODO Auto-generated method stub } @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
  16. 16. 16 ServletException, IOException { TODO Auto-generated method stub String username = request.getParameter("id"); String password = request.getParameter("password"); System.out.println("username="+username); System.out.println("password="+password); connectdb = new connect(); try { db.createConnection(); } catch (SQLException e) { TODO Auto-generated catch block e.printStackTrace(); } } }
  17. 17. 17 4.3) Java.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </h1>
  18. 18. 18 <center> <form action="LoginServlet" method="post" onsubmit="return check()"> <table> <tr> <td align="right">User</td> <td><input type="text" name="id" ><br></td> </tr> <tr> <td align="right">Password</td> <td><input type="password" name="password" ><br></td> </tr> <tr> <td align="center" colspan="2">
  19. 19. 19 <input type="submit" name="ok" value=" > <br></td> <!-- <tr> <td align="right"> </td> <td><input type="text" name="Firstname" id="Firstname"><br></td> </tr> <tr> <td align="right"> </td> <td><input type="text" name="lastname" id="lastname"><br></td> </tr> <tr> <td align="right"> </td>
  20. 20. 20 <td><input type="radio" checked="checked" name="sex" value=" > <input type="radio" name="sex" value=" > <br></td> </tr> <tr> <td align="right"> </td> <td> <input type="checkbox" name="cartoon" value=" > <input type="checkbox" name="cartoon" value=" > <input type="checkbox" name="cartoon" value=" > <input type="checkbox" name="cartoon" value=" > <br></td></tr>
  21. 21. 21 <tr> <td align="right" > </td> <td><textarea name="Address" id="Address"></textarea><br></td> </tr> <tr> <td align="right"> </td> <td><select name="Province"> <option value=" > </option> <option value=" > </option> <option value=" > </option> <option value=" > </option> <option value=" > </option> <option value=" > </option>
  22. 22. 22 </select><br></td> </tr> <tr> <td align="right"> </td> <td><input type="text" name="number" id="number"><br></td> </tr> <tr> <td align="right"> </td> <td><input type="text" name="email" id="email"><br></td> </tr> <tr> <td align="right"> </td> <td><input type="file" id="image"><br></td>
  23. 23. 23 </tr> <tr> <td align="center" colspan="2"> <input type="submit" name="ok" value=" > <input type="reset"name="delete" value=" > <br></td> </tr> </table> </form></center> </body></html> 5. MySQL Navicat 1.
  24. 24. 24 Navicat Connection Connection Connection Name: Connection Host name/IP address: localhost Port: User name: User name MySQL root Password: MySQL Test Connection Conection Successfully OK 2.
  25. 25. 25 connection MySQL connection New Database Create New Database Enter database name: Character set: utf8–UTF-8 Unicode
  26. 26. 26 Collation: Collation utf8_general_ci OK Navicat 1. Navicat Tables => New Table
  27. 27. 27
  28. 28. 28
  29. 29. 29

×