请教高手,帮我看看连接MYSQL的代码哪错了????
前面一段是对的,可以用!!!!为什么后面一段我的就连不上,真气人啊!!importjava.sql.Connection;importjava.sql.DriverMan...
前面一段是对的,可以用!!!!为什么后面一段我的就连不上,真气人啊!!
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true";
public Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
System.out.println(conn);
}
}
我的代码
package cao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import chaoren.GoodsForm;
import java.sql.*;
public class GoodsAction extends Action {
GoodsForm goodsform=null;
private int categoreyId =0;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
goodsform=new GoodsForm();
categoreyId=Integer.parseInt(request.getParameter("id"));
String dbDriver = "com.mysql.jdbc.Driver"; // 数据库的驱动
String url="jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true"; // URL地址
Connection connection = null;
try {
Class.forName(dbDriver); // 加载数据库驱动
connection = DriverManager.getConnection(url);// 加载数据库
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
PreparedStatement ps = null;
ps = connection.prepareStatement("select max(ID)+1 as ID from dept where categoreyId="+categoreyId+"");
ResultSet rs=ps.executeQuery();
while(rs.next())
goodsform=new GoodsForm();
goodsform.setCode(rs.getString("ID"));
System.out.println(goodsform.getCode());
request.setAttribute("codes", goodsform);
return mapping.findForward("good");}
}
错误提示:
3
数据库加载失败
2009-11-5 11:54:43 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2009-11-5 11:54:43 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
我有项目包,谁能帮忙看看的感激不禁 展开
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true";
public Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
System.out.println(conn);
}
}
我的代码
package cao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import chaoren.GoodsForm;
import java.sql.*;
public class GoodsAction extends Action {
GoodsForm goodsform=null;
private int categoreyId =0;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
goodsform=new GoodsForm();
categoreyId=Integer.parseInt(request.getParameter("id"));
String dbDriver = "com.mysql.jdbc.Driver"; // 数据库的驱动
String url="jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true"; // URL地址
Connection connection = null;
try {
Class.forName(dbDriver); // 加载数据库驱动
connection = DriverManager.getConnection(url);// 加载数据库
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
PreparedStatement ps = null;
ps = connection.prepareStatement("select max(ID)+1 as ID from dept where categoreyId="+categoreyId+"");
ResultSet rs=ps.executeQuery();
while(rs.next())
goodsform=new GoodsForm();
goodsform.setCode(rs.getString("ID"));
System.out.println(goodsform.getCode());
request.setAttribute("codes", goodsform);
return mapping.findForward("good");}
}
错误提示:
3
数据库加载失败
2009-11-5 11:54:43 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2009-11-5 11:54:43 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
我有项目包,谁能帮忙看看的感激不禁 展开
5个回答
展开全部
兄弟,同是天涯沦落人啊,我也是,气死我了,都!!,你要是搞出来了,记得告诉我一下,谢谢!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码太乱了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
categoreyId=Integer.parseInt(request.getParameter("id"));
?这个参数哪里来的
报的是空指针异常那么肯定有一个对象为空 或者一个值没获取到
?这个参数哪里来的
报的是空指针异常那么肯定有一个对象为空 或者一个值没获取到
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码太乱了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询