Use JDBC APIs in JSP to access the database
1 <% @ Page Language = " Java " Contenttype = " Text/html; charset = UTF-8 " 2 Pageencoding = " UTF-8 " %> 3 <% @ Page import = " Java. Io .* " %> 4 <% @ Page import = " Java. util .* " %> 5 <% @ Page import = " Java. SQL .* " %> 6 <! Doctype HTML public " -// W3C // dtd html 4.01 transitional // en " " Http://www.w3.org/TR/html4/loose.dtd " > 7 <HTML> 8 <Head> 9 <Meta http-equiv = " Content-Type " Content = " Text/html; charset = UTF-8 " > 10 <Title> testjdbcapi </title>11 </Head> 12 <Body> 13 <% 14 Try { 15 Connection con; 16 Statement stmt; 17 Resultset RS; 18 19 // MySQL Load 20 Class. forname ( " Com. MySQL. JDBC. Driver " ); 21 // Register MySQL (to have a MySQL jar package) 22 Drivermanager. registerdriver ( New Com. MySQL. JDBC. Driver ()); 23 // Connect to DB with appropriate driver 24 String dburl = " JDBC: mysql: // localhost: 3306/bookdb? Useunicode = true & characterencoding = UTF-8 " ; 25 String dbuser = " Username " ; 26 String dbpwd = " Password " ; 27 28 // Create DB's connection 29 Con = Java. SQL. drivermanager. getconnection (dburl, dbuser, dbpwd ); 30 // Create SQL statement 31 Stmt = Con. createstatement (); 32 // Add data 33 Stmt.exe cuteupdate (" Insert specific insert statements " ); 34 35 // Select data 36 Rs = stmt.exe cutequery ( " Select statement " ); 37 38 // Out select result 39 Out . Println ( " <Table border = 1 width = 400> " ); 40 While (Rs. Next ()){ 41 String col1 = Rs. getstring ( 1 ); 42 String col2 = Rs. getstring ( 2 ); 43 String col3 = Rs. getstring ( 3 ); 44 Float Col4 = Rs. getfloat ( 4 ); 45 46 // Print datas 47 Out . Println ( " <Tr> <TD> " + Col1 + " </TD> " 48 + " <TD> " + Col2 + " </TD> " 49 + " <TD> " + Col3 + " </TD> " 50 + " <TD> " + Col4 + " </TD> </tr> " ); 51 } 52 Out . Println ( " </Table> " ); 53 54 // Delete datas 55 Stmt.exe cuteupdate ( " Delete statement " ); 56 57 // Close 58 Rs. Close (); 59 Stmt. Close (); 60 Con. Close (); 61 62 } Catch (Exception e ){ 63 Out . Println (E. getmessage ()); 64 } 65 %> 66 </Body> 67 </Html>