初探JDBC

MYSQL的版本
C:\mysql\bin>mysql.exe --version
mysql.exe Ver 14.14 Distrib 5.1.29-rc, for Win32 (ia32)

TOMCAT的版本
Tomcat 5.5

JDBC的驱动用的是这个
mysql-connector-java-5.1.7-bin.jar


配置代码

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.ResultSetMetaData" %>

<%
String drv = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/demo";
String usr = "root";
String pwd = "woody";
%>


测试代码

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ include file="inc.jsp" %>


<%String sql = "select * from user";
try {
Class.forName(drv).newInstance();
Connection conn = DriverManager.getConnection(url, usr, pwd);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
String field = (String) (rsmd.getColumnName(i));
String value = (String) (rs.getString(i));
out.print(field + "=" + value + ";");
}
}
rs.close();
stm.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.println(e);
}
%>

你可能感兴趣的:(初探JDBC)