Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page import="java.sql.*" %>
- <%@ page import="javax.servlet.http.*" %>
- <%! String runQuery(String name) throws SQLException {
- Connection conn = null;
- PreparedStatement stmt = null;
- ResultSet rset = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "test", "test");
- stmt = conn.prepareStatement("INSERT INTO users VALUES (999, ?);");
- stmt.setString(1, name);
- stmt.executeUpdate();
- return "success";
- } catch (Exception e) {
- return ("<P> Error: <PRE> " + e + " </PRE> </P>n");
- } finally {
- if (rset!= null) rset.close();
- if (stmt!= null) stmt.close();
- if (conn!= null) conn.close();
- }
- }
- %>
- <%
- request.setCharacterEncoding("utf8");
- String name = request.getParameter("name");
- if (name != null) {
- out.print(runQuery(name));
- }
- %>
- mysql> show create table users;
- +-------+--------------------------------------------------------------------------------------------------------+
- | Table | Create Table |
- +-------+--------------------------------------------------------------------------------------------------------+
- | users | CREATE TABLE `users` (
- `id` int(11) DEFAULT NULL,
- `name` text
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
- +-------+--------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
Add Comment
Please, Sign In to add comment