How to connect jsp to mysql: first download the JDBC driver from the mysql official website; then put the jar package into the lib folder of the tomcat installation directory; then create test data in mysql; finally create it in eclipse Project and create a JSP file to run.
Recommended: "mysql video tutorial"
jsp connection MYSQL database tutorial
Steps:
1. Download the JDBC driver from the mysql official website. URL: https://dev.mysql.com/downloads/connector/j/
2. Put the jar package (mysql-connector-java-5.1.41-bin.jar) into tomcat Under the lib folder of the installation directory, for example, my directory is: C:\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\lib. Restart tomcat.
3. Create test data in mysql. First create a database named testdb; then create a table named testform. The table has three fields: id, name, age; finally add a few lines of tests data.
4.Create a project in eclipse and create a JSP file named test.jsp. The test.jsp code is as follows: (Note to modify the corresponding parameters)
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <% //驱动程序名 String driverName = "com.mysql.jdbc.Driver"; //数据库用户名 String userName = "root"; //密码 String userPasswd = "123456"; //数据库名 String dbName = "testdb"; //表名 String tableName = "testform"; //联结字符串 String url = "jdbc:mysql://localhost/" + dbName + "?user=" + userName + "&password=" + userPasswd; //加载驱动 Class.forName("com.mysql.jdbc.Driver").newInstance(); //建立连接 Connection conn = DriverManager.getConnection(url); //创建Statement(负责执行sql语句) Statement stmt = conn.createStatement(); String sql="select * from " + tableName; //获得数据结果集合 ResultSet rs = stmt.executeQuery(sql); //依次遍历结果集(表中的记录) while(rs.next()) { //依据数据库中的字段名打印数据 out.println(rs.getString("name")); out.println(rs.getString("age")); } //关闭连接 rs.close(); stmt.close(); conn.close(); %>
5. Run the jsp file.
The above is the detailed content of jsp link mysql step method. For more information, please follow other related articles on the PHP Chinese website!