求助,一个SQLException
jsp可以在Mysql中创建表,但是不能再表中创建数据,求高手解答!小菜感激不尽!代码如下:
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>创建数据库</title>
</head>
<body><font size=2>
<%
Connection conn=null;
Statement stmt=null;
String sql=null;
ResultSet rs=null;
int ret;
try{
Class.forName("com.mysql.jdbc.Driver");
String strCon="jdbc:mysql://localhost:3306/";
conn=DriverManager.getConnection(strCon,"root","root");
stmt=conn.createStatement();
sql="create database JspSamples";
ret=stmt.executeUpdate(sql);
sql="use JspSamples";
ret=stmt.executeUpdate(sql);
sql="CREATE TABLE customers("+
"ID int(6) unsigned NOT NULL auto_increment,"+
"Name varchar(20) default NULL,"+
"addTime timestamp NULL default NULL,"+
"Tel varchar(15) default NULL,"+
"Email varchar(20) default NULL,"+
"PRIMARY KRY (Id)"+
")";
ret=stmt.executeUpdate(sql);
}
catch (ClassNotFoundException e){
e.printStackTrace();
out.println("<h1>无法找到数据库驱动程序</h1>");
}
catch (SQLException e1){
e1.printStackTrace();
out.print("<h1>数据库操作失败</h1>");
}
out.println("<h1>新建数据库成功</h1>");
DatabaseMetaData meta=conn.getMetaData();
out.println("<br>");
out.println("连接字: "+meta.getURL());
out.println("<br>");
out.println("数据库产品: "+meta.getDatabaseProductName());
out.println("<br>");
out.println("数据库版本: "+meta.getDatabaseProductVersion());
out.println("<br>");
out.println("驱动程序: "+meta.getDriverName());
out.println("<br>");
out.println("驱动程序版本: "+meta.getDriverVersion());
stmt.close();
conn.close();
%>
</font>
</body>
</html> --------------------编程问答-------------------- 上面的是创建表的代码,还有一个添加数据的代码:如下:
<%@ page language="java" contentType="text/html; charset=Gb2312"
pageEncoding="GB2312"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>添加数据</title>
</head>
<body>
<h2>添加客户信息</h2>
<form name="form1" method="post" action="Ex5_2.jsp?action=submit">
<table width="59%" height="156" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="21%">客户名:</td>
<td width="79"><input name="name" type="text" size="10"></td>
</tr>
<tr>
<td>电话: </td>
<td> <input name="tel" type="text" size="10"></td>
</tr>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="10"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="登录"></td>
</tr>
</table>
</form>
<font size=2>
<%
Connection conn=null;
Statement stmt=null;
String sql=null;
ResultSet rs=null;
int ret;
String action=request.getParameter("action");
if("submit".equals(action)){
String name=request.getParameter("name");
String tel=request.getParameter("tel");
String email=request.getParameter("email");
Timestamp addtime=new Timestamp(System.currentTimeMillis());
try{
Class.forName("com.mysql.jdbc.Driver");
String strCon="jdbc:mysql://localhost:3306/JspSamples";
conn=DriverManager.getConnection(strCon,"root","root");
stmt=conn.createStatement();
sql = " insert into customers (Name, Tel, addTime, Email)values('"+name+"','"+tel+"','"+addtime+"','"+email+"') ";
ret =stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (ClassNotFoundException e){
e.printStackTrace();
out.println("<h1>无法找到数据库驱动</h1>");
}catch (SQLException e1){
e1.printStackTrace();
out.println("<h1>数据库操作失败</h1>");
}
out.println("<h1>插入数据到JspSamples的customers表成功</h1>");
}
%></font>
</body>
</html> --------------------编程问答-------------------- 求助啊!!!! --------------------编程问答-------------------- 把错误代码贴出来 --------------------编程问答-------------------- 贴出来了啊,就在上面啊
补充:Java , Java相关