Tomcat JSP to access the mysql database:
1. Install the Tomcat and MySqlshuu to download mysql-connector-java.zip, then decompress two files, I download my mysql-connection-java-5.0.0-beta, so two files are mysql-connector-java -5.0.0-beta-bin.jar and mysql-connector-java-5.0.0-beta-bin-g.jar;
2. Copy mysql-connector-java-5.0.0-beta-bin.jar to the Apache-Tomcat / Common / Lib directory, restart Tomcat;
3, write a JSP file, such as Test.jsp:
<% @ Page Import = "java.io. *"%> <% @ page import = "java.util. *"%> <% @ page import = "java.sql. *"%> <% @ page contenttype = "text / html; charSet = GB2312"%>
Class.Forname ("com.mysql.jdbc.driver"); //class.forname ("org.gjt.mm.mysql.driver "). NewInstance ();
DriverManager.registerDriver (new com.mysql.jdbc.Driver ()); String dbUrl = "jdbc: mysql: // localhost:? 3306 / BookDB userUnicode = true & characterEncoding = GB2312"; String dbUser = "user"; String dbPwd = " 123456 "; con = java.sql.driverManager.getConnection (DBURL, DBUSER, DBPWD); stmt = con.createstatement (); rs = stmt.executeQuery (" Select ID, Name, Title, Price from Books "); OUT. Println ("