连接数据库程序
<%@ 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="111111";
//数据库名
String dbName="test";
//表名
String tableName="user";
//字段值
//联结字符串
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();
ResultSet rs = statement.executeQuery("select * FROM "+tableName);
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount();
// 输出每一个数据值
/*<table border>
<tr><th>姓名</th><th>密码</th>
<tr><td>A</td><td>B</td>
<tr><td>A</td><td>B</td>
</table>*/
out.print("<table border>");
out.print("<th>");
out.print("<tr>");
out.print("<td>");
out.print("姓 名");
out.print("</td>");
out.print("<td>");
out.print("密 码");
out.print("</td>");
out.print("</tr>");
while(rs.next()) {
out.print("</tr>");
out.print("<td>");
out.print(rs.getString(1)+" ");
out.print("</td>");
out.print("<td>");
out.print(rs.getString(2));
out.print("</td>");
out.print("</tr>");
}
out.print("</th>");
out.print("</table>");
out.print("<button>");
out.print("新 增");
out.print("</button>");
out.print("<button>");
out.print("修 改");
out.println("</button>");
out.print("</br>");
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>