Tomcat connects to the mysql database Tomcat
Today, I tried to connect to the mysql database. after successful connection, I wrote something. after all, the road to growth is still long.
<% @ Page contentType = "text/html" language = "java" errorPage = "" pageEncoding = "UTF-8" %> <% @ page import = "java. SQL. * "%>Welcome to xasasa<% // Register the database driver Class. forName ("com. mysql. jdbc. driver "); // Get the database Connection conn = DriverManager. getConnection ("jdbc: mysql: // mystruts2", "root", "leebt"); // create StatementStatement stmt = conn. createStatement (); // execute the query ResultSet rs = stmt.exe cuteQuery ("select * from students"); %>
<% // Traverse the result set while (rs. next () {%>
<% = Rs. getString (1) %> |
<% = Rs. getString (2) %> |
<% }%>
This is the content of index. jsp. Mystuts2 is the database name and students is the table name. The specific values are inserted in mysql.
This is the result. In short, the code is not too complex and the principle is the most important.