jsp向mysql提交表单
<%StringName=request.getParameter("Name");StringPassword=request.getParameter("passwo...
<%
String Name=request.getParameter("Name");
String Password=request.getParameter("password1");
String RealName=request.getParameter("realname");
String Sex=request.getParameter("sex");
String Age=request.getParameter("age");
String Job=request.getParameter("job");
String Email=request.getParameter("email");
String Phone=request.getParameter("phone");
String Address=request.getParameter("address");
String QQ=request.getParameter("qq");
Statement stmt=con.createStatement();
rs=stmt.executeQuery(insert into tb_Account (Name) values('"+Name+"'));
stmt.close();
con.close();//关闭连接、释放资源
%>
这是我的代码 每次运行都是错的 。。。 主要目的就是向从jsp页面获取一个表单 提交到Mysql数据库中 求高人啊 展开
String Name=request.getParameter("Name");
String Password=request.getParameter("password1");
String RealName=request.getParameter("realname");
String Sex=request.getParameter("sex");
String Age=request.getParameter("age");
String Job=request.getParameter("job");
String Email=request.getParameter("email");
String Phone=request.getParameter("phone");
String Address=request.getParameter("address");
String QQ=request.getParameter("qq");
Statement stmt=con.createStatement();
rs=stmt.executeQuery(insert into tb_Account (Name) values('"+Name+"'));
stmt.close();
con.close();//关闭连接、释放资源
%>
这是我的代码 每次运行都是错的 。。。 主要目的就是向从jsp页面获取一个表单 提交到Mysql数据库中 求高人啊 展开
4个回答
展开全部
这基本上是
JDBC的问题啦
我给你发我经常用的一个DatabaseConnecter的代码你参考一下
这种情况一般都用javabean的直接在jsp上写不好
package flight.beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author zhan
*/
public class DatabaseConnecter {
//这里改密码和用户名还有选择要连接的Database
private String connecter = "jdbc:mysql://127.0.0.1/flight?user=root&password=1234";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public void connectDatabse() {
conn = this.getConnecction();
stmt = this.getStatement(conn);
}
public Connection getConnecction() {
Connection c = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(connecter);
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
}
return c;
}
public Statement getStatement(Connection c) {
Statement s = null;
try {
s = c.createStatement();
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
}
return s;
}
public ResultSet getResultSet(Statement s, String sql) {
ResultSet r = null;
try {
r = s.executeQuery(sql);
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return r;
}
public void updateData(String sql) {
this.connectDatabse();
try {
stmt.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
} finally {
this.closeDatabase();
}
}
public void closeDatabase() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
JDBC的问题啦
我给你发我经常用的一个DatabaseConnecter的代码你参考一下
这种情况一般都用javabean的直接在jsp上写不好
package flight.beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author zhan
*/
public class DatabaseConnecter {
//这里改密码和用户名还有选择要连接的Database
private String connecter = "jdbc:mysql://127.0.0.1/flight?user=root&password=1234";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public void connectDatabse() {
conn = this.getConnecction();
stmt = this.getStatement(conn);
}
public Connection getConnecction() {
Connection c = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(connecter);
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
}
return c;
}
public Statement getStatement(Connection c) {
Statement s = null;
try {
s = c.createStatement();
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
}
return s;
}
public ResultSet getResultSet(Statement s, String sql) {
ResultSet r = null;
try {
r = s.executeQuery(sql);
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return r;
}
public void updateData(String sql) {
this.connectDatabse();
try {
stmt.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
} finally {
this.closeDatabase();
}
}
public void closeDatabase() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以去你的数据库看一下,是不是其它的列都设置的不能为空?或是你报的是不是空指针的错误呢?而且一般情况下JSP是不这样写的,最有可能错的地方就是
rs=stmt.executeQuery(insert into tb_Account (Name) values("+Name+"'));
应该使用stmt.executeUpdate你用这个方法试一下吧
rs=stmt.executeQuery(insert into tb_Account (Name) values("+Name+"'));
应该使用stmt.executeUpdate你用这个方法试一下吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
rs=stmt.executeQuery(insert into tb_Account (Name) values('"+Name+"'));应该是这一句有错。
stmt.executeQuery("insert into tb_Account(Name) values('"+Name+"')");
stmt.executeQuery("insert into tb_Account(Name) values('"+Name+"')");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
stmt.executeQuery改为stmt.executeUpdate
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询