public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password; public void init() throws ServletException
{
String driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch (ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
stmt=conn.createStatement();
stmt.executeUpdate("create datebase bookstore");
stmt.executeUpdate("use bookstore");
stmt.executeUpdate("create table bookinfo(id INT not null primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish-data DATA not null,price FLOAT(4,2) not null,amount SMALLINT,re VARCHAR(200))ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'java Web开发','孙鑫','电子工业出版社','2006-4-20',99.00,35,null)");
stmt.executeBatch();
resp.setContentType("text/html;charset=GBK");
PrintWriter out=resp.getWriter();
out.println("数据库加载成功");
out.close();
}
catch (SQLException se)
{
throw new ServletException(se);
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch (SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch (SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
<?xml version="1.0" encoding="gb2312"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><servlet>
<servlet-name>CreateDBServlet</servlet-name>
<servlet-class>CreateDBServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>12345678</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CreateDBServlet</servlet-name>
<url-pattern>/createdb</url-pattern>
</servlet-mapping></web-app>启动TOMCAT之后 访问http://localhost:8080/ch04/createdb出现如下错误
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
CreateDBServlet.doGet(CreateDBServlet.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.6 logs.
初次使用数据库,还请指点 不胜感激
{
private String url;
private String user;
private String password; public void init() throws ServletException
{
String driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch (ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
stmt=conn.createStatement();
stmt.executeUpdate("create datebase bookstore");
stmt.executeUpdate("use bookstore");
stmt.executeUpdate("create table bookinfo(id INT not null primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish-data DATA not null,price FLOAT(4,2) not null,amount SMALLINT,re VARCHAR(200))ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'java Web开发','孙鑫','电子工业出版社','2006-4-20',99.00,35,null)");
stmt.executeBatch();
resp.setContentType("text/html;charset=GBK");
PrintWriter out=resp.getWriter();
out.println("数据库加载成功");
out.close();
}
catch (SQLException se)
{
throw new ServletException(se);
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch (SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch (SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
<?xml version="1.0" encoding="gb2312"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><servlet>
<servlet-name>CreateDBServlet</servlet-name>
<servlet-class>CreateDBServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>12345678</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CreateDBServlet</servlet-name>
<url-pattern>/createdb</url-pattern>
</servlet-mapping></web-app>启动TOMCAT之后 访问http://localhost:8080/ch04/createdb出现如下错误
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
CreateDBServlet.doGet(CreateDBServlet.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.6 logs.
初次使用数据库,还请指点 不胜感激
解决方案 »
- 用js 写随机数的注册验证代码,怎么写?如:3+5=?
- 我用JSP做了个购物车的代码,在有个页面出了问题 希望各位帮我看看额
- 200分求解,关于通过 get/post 方式从 browser 返回的参数值中包含汉字信息的问题
- 如何在过滤类中修改用户的访问地址?
- 大家好,按字数分页怎么实现呢?高分相送
- oracle数据库配置jndi与hibernate的问题。配置了一晚上都弄不好,请大家帮帮忙!
- jsp中文乱码问题?》
- ■■看了那个用javabean上传文件的例程,有以下看法,不知对否,请教,参与有分!■■
- 我使用jspSmartUpload上载图片到Oracle数据库中,现在传小图片没问题,超过5K后就有问题。(请高手解答,必有重谢)
- 120求助有关cookie的问题
- SSH保存记录出错,找不到指针
- 刚刚接触XMPP协议,想做一个即时通信程序
the web application [/ch04] registered the JDBC driver [com.mysql.jdbc.Driver] but failes to unregister it when the web qpplication was stopped. To prevent a memory leak,the JDBC Driver has been forcibly unregistered
你已经获得了数据库连接了,但是还报错,我想是你的WEB.XML里没有配置数据库。 <init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/数据库名字</param-value>
</init-param>我的清单:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>CreateDBServlet</servlet-name>
<servlet-class>com.zhuchao.servlet.CreateDBServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://127.0.0.1:3306/hibernate</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>zhuchao</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CreateDBServlet</servlet-name>
<url-pattern>/CreateDBServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>package com.zhuchao.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CreateDBServlet extends HttpServlet {
private static final long serialVersionUID = 4504905042789200456L;
private static String driverClass;
private static String url;
private static String user;
private static String password;
public CreateDBServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
System.err.println(driverClass+url+user+password);
Connection connection=null;
try {
Class.forName(driverClass);
connection = DriverManager.getConnection(url,user,password);
if (connection!=null) {
System.err.println("获的连接");
int i=0;
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM tb_company WHERE LEVEL =0 ");
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
i=resultSet.getInt(1);
System.err.println("结果:"+i);
}
resultSet.close();
preparedStatement.close();
connection.close();
connection.close();
}else {
System.err.println("未获取连接");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
}
}我的结果:
com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/hibernaterootzhuchao
获的连接
结果:6
stmt=conn.createStatement();
你没初始化conn就用它了 当然会空指针了
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: java.sql.SQLException: Can't create database 'bookstore'; database exists
CreateDBServlet.doGet(CreateDBServlet.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause java.sql.SQLException: Can't create database 'bookstore'; database exists
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)
CreateDBServlet.doGet(CreateDBServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.6 logs.怎么无法创建bookstore呢
你把
create table bookinfo(id INT not null primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish-data DATA not null,price FLOAT(4,2) not null,amount SMALLINT,re VARCHAR(200))ENGINE=InnoDB放到查询分析器里 执行一边 如果 可以创建数据库 那就能行了。
这句,已经存在'bookstore'这个database了,改名字或者在database里面删去