form表单数据保存到mysql中的问题?
mysql中在建了一个库,名为test>再建了一个表(info):createtablebiao(idint(8)primarykey,namevarchar(10),p...
mysql中在建了一个库,名为test>再建了一个表(info):create table biao(id int(8) primary key,name varchar(10),password varchar(100));
下面是新建的LinkMySQL.jsp,我的数据库的密码是8888,。驱动我放在lib下,
<%@ page language="java" import="java.util.*"page import="com.mysql.jdbc.Driver"
import="java.sql.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'LinkMySQL.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="8888";
//数据库名
String dbName="test";
//表名
String tableName="info";
//联结字符串
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();
// 输出每一个数据值
out.print("id");
out.print("|");
out.print("num");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString(1)+" ");
out.print("|");
out.print(rs.getString(2));
out.print("<br>");
}
out.print("<br>");
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>
</body>
</html>
帮忙,谢谢。只是想测试下,表单中的信息怎么存入数据库(mysql),然后在读取出来。 展开
下面是新建的LinkMySQL.jsp,我的数据库的密码是8888,。驱动我放在lib下,
<%@ page language="java" import="java.util.*"page import="com.mysql.jdbc.Driver"
import="java.sql.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'LinkMySQL.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="8888";
//数据库名
String dbName="test";
//表名
String tableName="info";
//联结字符串
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();
// 输出每一个数据值
out.print("id");
out.print("|");
out.print("num");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString(1)+" ");
out.print("|");
out.print(rs.getString(2));
out.print("<br>");
}
out.print("<br>");
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>
</body>
</html>
帮忙,谢谢。只是想测试下,表单中的信息怎么存入数据库(mysql),然后在读取出来。 展开
1个回答
2012-03-30
展开全部
把整个表单的数据全部一下子存入数据库,并不是一个很好的设计,很少有人要这样做,往往是一行一行的保存。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询