这基本上是
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.executeQuery("insert into tb_Account(Name) values('"+Name+"')");
stmt.executeQuery改为stmt.executeUpdate