servletからmysqlにinsert

servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		
		// import java.sql.*;
//		Connection db_con = null;
//		PreparedStatement db_st = null;
//		ResultSet db_data = null;
		
		Connection conn = null;
		ResultSet rs = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection("jdbc:mysql://localhost/sampledb","root","");

			Statement stmt = conn.createStatement();
			String sql = "insert into userinfo(userid, status) values ('hoge', 1)";
			int num = stmt.executeUpdate(sql);
			stmt.close();
		} catch(Exception e) {
			  e.printStackTrace();
		}
		finally {
			
		}
		
		
		
	}

mysql> select * from userinfo;
+———+——–+
| userid | status |
+———+——–+
| aaaa123 | 0 |
| asdf123 | 1 |
| hoge | 1 |
| xyzx123 | 2 |
+———+——–+
4 rows in set (0.01 sec)

なるほど♪