Guest User

Untitled

a guest
Dec 20th, 2017
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="javax.servlet.http.*" %>
  3.  
  4. <%! String runQuery(String name) throws SQLException {
  5. Connection conn = null;
  6. PreparedStatement stmt = null;
  7. ResultSet rset = null;
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. conn = DriverManager.getConnection(
  11. "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "test", "test");
  12.  
  13. stmt = conn.prepareStatement("INSERT INTO users VALUES (999, ?);");
  14. stmt.setString(1, name);
  15. stmt.executeUpdate();
  16. return "success";
  17. } catch (Exception e) {
  18. return ("<P> Error: <PRE> " + e + " </PRE> </P>n");
  19. } finally {
  20. if (rset!= null) rset.close();
  21. if (stmt!= null) stmt.close();
  22. if (conn!= null) conn.close();
  23. }
  24. }
  25. %>
  26.  
  27. <%
  28. request.setCharacterEncoding("utf8");
  29. String name = request.getParameter("name");
  30. if (name != null) {
  31. out.print(runQuery(name));
  32. }
  33. %>
  34.  
  35. mysql> show create table users;
  36. +-------+--------------------------------------------------------------------------------------------------------+
  37. | Table | Create Table |
  38. +-------+--------------------------------------------------------------------------------------------------------+
  39. | users | CREATE TABLE `users` (
  40. `id` int(11) DEFAULT NULL,
  41. `name` text
  42. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  43. +-------+--------------------------------------------------------------------------------------------------------+
  44. 1 row in set (0.00 sec)
Add Comment
Please, Sign In to add comment