Jsp connection to mysql database, jspmysql Database
1. Create a Java web project.
2. Import the mysql driver package. (This is similar to the Java connection to mysql written in the previous article)
3. Write test code
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %>
<% @ Page language = "java" %>
<% @ Page import = "com. mysql. jdbc. Driver" %>
<% @ Page import = "java. SQL. *" %>
<%
String driverName = "com. mysql. jdbc. Driver ";
String userName = "root ";
String userPasswd = "root ";
String dbName = "goods ";
String tableName = "t_admin ";
String url = "jdbc: mysql: // localhost/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection connection = DriverManager. getConnection (url );
Statement statement = connection. createStatement ();
String SQL = "SELECT * FROM" + tableName;
ResultSet rs = statement.exe cuteQuery (SQL );
ResultSetMetaData rmeta = rs. getMetaData ();
Int numColumns = rmeta. getColumnCount ();
Out. print ("id ");
Out. print ("| ");
Out. print ("num ");
Out. print ("<br> ");
While (rs. next ()){
Out. print (rs. getString (1) + "");
Out. print ("| ");
Out. print (rs. getString (2 ));
Out. print ("<br> ");
}
Out. print ("<br> ");
Out. print ("database operation successful, congratulations ");
Rs. close ();
Statement. close ();
Connection. close ();
%>
4. Copy the mysql driver package to the lib directory of tomcat.
5. Open your browser and run the test file.