一、相关软件
这个不用说了吧
1. mysql
2. mysql-connector-java-5.1.22.jar
地址:http://dev.mysql.com/downloads/connector/j/
3. tomcat
二、连接要点
只需要将 mysql-connector-java-5.1.22.jar 放在 tomcat的lib目录下,即可。
如本人的:D:\ProgramFiles\WEB\apache-tomcat\lib\mysql-connector-java-5.1.22.jar
三、参考代码
<%@page contentType="text/html;charset=utf-8" %> <%@page import="java.sql.*" %> <html> <body> <% Connection con; Statement sql; ResultSet rs; try{ Class.forName("org.gjt.mm.mysql.Driver");//load database Driver out.println("Driver loading success<br>"); }catch(ClassNotFoundException e){ out.println("Driver loading fail<br>"); } String url="jdbc:mysql://localhost:3306/YourDatabase"; String userName = "YourUserName"; String passWord = "YourPassword"; try { con = DriverManager.getConnection(url, userName, passWord); if (con != null) { out.println("Succeed connecting to the database<br>"); sql = con.createStatement(); rs = sql.executeQuery("select * from YourTable"); while(rs.next()){ out.println("Output Demo: "+rs.getString(2)); } } }catch(SQLException e){ out.println(e.getMessage()); } %> </body> </html>