做了一个简单的注册页面,下面是JAVA代码,老是int rs = stmt.executeUpdate(upstr); 这一行出现错误
这是java代码,求各位大神帮助,谢谢packagedb;importjava.sql.*;publicclassfunction{privatestaticConnec...
这是java代码,求各位大神帮助,谢谢
package db;
import java.sql.*;
public class function {
private static Connection con = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public void connect(){
String url = "jdbc:mysql://127.0.0.1:3306/studentdb?useUnicode=true&characterEncoding=gb2312";
//在这里修改数据库名
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password ="";
try {
Class.forName("com.mysql.jdbc.Driver");//在这里修改数据库
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, user, password);
//在这里修改数据库Mysql的信息
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet select(String query)
{
System.out.println(query);
try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void update(String upstr) {
System.out.println(upstr);
try {
stmt = con.createStatement();
int rs = stmt.executeUpdate(upstr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close()
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
信息存到数据库名为studentdb的student表中,这是register.jsp部分代码,运行java,显示The requested resource is not available.
<% String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
String major = request.getParameter("major");
String college = request.getParameter("college");
ResultSet rs=null;
String str="select id,name,age,major,college from student where name='"+name+"'";
query.connect();
rs=query.select(str);
while(rs!=null){
rs.last();
}
str="insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')";
query.update(str);
rs=query.select(str);
while(rs!=null){
rs.last();
}
out.println("提交信息成功!");
out.print("本页面将在3秒后自动跳转到上一页面!");
out.print("<meta http-equiv="+"refresh"+" content=" +"3;url=register.jsp"+">"); %>
</body></html> 展开
package db;
import java.sql.*;
public class function {
private static Connection con = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public void connect(){
String url = "jdbc:mysql://127.0.0.1:3306/studentdb?useUnicode=true&characterEncoding=gb2312";
//在这里修改数据库名
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password ="";
try {
Class.forName("com.mysql.jdbc.Driver");//在这里修改数据库
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, user, password);
//在这里修改数据库Mysql的信息
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet select(String query)
{
System.out.println(query);
try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void update(String upstr) {
System.out.println(upstr);
try {
stmt = con.createStatement();
int rs = stmt.executeUpdate(upstr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close()
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
信息存到数据库名为studentdb的student表中,这是register.jsp部分代码,运行java,显示The requested resource is not available.
<% String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
String major = request.getParameter("major");
String college = request.getParameter("college");
ResultSet rs=null;
String str="select id,name,age,major,college from student where name='"+name+"'";
query.connect();
rs=query.select(str);
while(rs!=null){
rs.last();
}
str="insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')";
query.update(str);
rs=query.select(str);
while(rs!=null){
rs.last();
}
out.println("提交信息成功!");
out.print("本页面将在3秒后自动跳转到上一页面!");
out.print("<meta http-equiv="+"refresh"+" content=" +"3;url=register.jsp"+">"); %>
</body></html> 展开
3个回答
展开全部
把sql打印出来看看,运行一下你的sql,看能不能成功。
把这个sql语句:insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')"; 直接在数据库里运行下看看能不能成功。
把这个sql语句:insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')"; 直接在数据库里运行下看看能不能成功。
追问
运行register.jsp结果如下:
description:The server encountered an internal error that prevented it from fulfilling this request.
java.lang.NullPointerException
db.function.select(function.java:32)
org.apache.jsp.add_jsp._jspService(add_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
展开全部
你把你的sql语句写出来,这个应该是sql语句写错了。
把异常信息贴出来吧。
把异常信息贴出来吧。
追问
运行register.jsp结果如下:
description:The server encountered an internal error that prevented it from fulfilling this request.
java.lang.NullPointerException
db.function.select(function.java:32)
org.apache.jsp.add_jsp._jspService(add_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
追答
query是个空对象,你没赋值吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这不明摆着db.function.select(function.java:32)。32行空指针了么
追问
谢谢你的回答啊,那应该在db.function.java中怎么改
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询