jsp连接mysql

使用tomcat服务器

需要把mysql-connector-java-5.0.4-bin.jar放在/usr/local/tomcat/lib目录下。


<%@ page contentType="text/html; charset=gb2312" %>

<%@ page language="java" %>

<%@ page import="com.mysql.jdbc.Driver" %>

<%@ page import="java.sql.*" %>

<%

String driverName="com.mysql.jdbc.Driver";

String userName="root";

String userPasswd="";

String dbName="test";

String tableName="t1";

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.executeQuery(sql);

ResultSetMetaData rmeta = rs.getMetaData();

int numColumns=rmeta.getColumnCount();

while(rs.next()) {

        out.print(rs.getString("id")+" ");

        out.print("|");

        out.print(rs.getString("name"));

        out.print("|");

        out.print(rs.getString("content"));

        out.print("<br>");

}

out.print(numColumns);

rs.close();

statement.close();

connection.close();

%>


你可能感兴趣的:(mysql,服务器,local)