初学JDBC

    刚开始学JDBC,记录下使用过程:

	public class TestJDBC extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("text/html;charset=GB2312");
		PrintWriter out = resp.getWriter();
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			
			String dbUrl = "jdbc:mysql://localhost:3306/mydata";
			String dbUser = "root";
			String dbPassword = "123456";
					
			Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select * from users");
			
			out.println("<html><head><title>my title</title></head>");
			out.println("<body>");
			out.println("<table>");
			
			while(rs.next()){
				out.println("<tr>");
				out.println("<td>" + rs.getString(1) + "</td>" + "<td>" + rs.getString(2) + "</td>");
				out.println("</tr>");
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		out.println("</table>");
		out.println("</body></html>");

		out.flush();
		out.close();
		
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}

你可能感兴趣的:(jdbc)