最近在看孙鑫的javaweb,在本机上安装好mysql后,将工程导入了tomcat,打开了mysql服务器,运行了tomcat,输入http://localhost:8080/ch08/createdb后无法显示sucesss,发现出错,显示unknown database 'bookstore'
本人代码
package org.sunxin.lesson.jsp.bookstore;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
String driverClass=getServletContext().getInitParameter("driverClass");
url=getServletContext().getInitParameter("url");
user=getServletContext().getInitParameter("user");
password=getServletContext().getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new UnavailableException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("create database 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_date DATE not null,price FLOAT(4,2) not null,amount SMALLINT,re VARCHAR(200)) ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'Java从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");
stmt.addBatch("insert into bookinfo values(2,'JSP深入编程','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");
stmt.executeBatch();
PrintWriter out=resp.getWriter();
out.println("success!");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
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;
}
}
}
}数据库TomcatServlet
本人代码
package org.sunxin.lesson.jsp.bookstore;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
String driverClass=getServletContext().getInitParameter("driverClass");
url=getServletContext().getInitParameter("url");
user=getServletContext().getInitParameter("user");
password=getServletContext().getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new UnavailableException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("create database 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_date DATE not null,price FLOAT(4,2) not null,amount SMALLINT,re VARCHAR(200)) ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'Java从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");
stmt.addBatch("insert into bookinfo values(2,'JSP深入编程','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");
stmt.executeBatch();
PrintWriter out=resp.getWriter();
out.println("success!");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
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;
}
}
}
}数据库TomcatServlet
解决方案 »
- 急救!!在线等答案!There is no Action mapped for namespace / and action name
- 神呀,救救我:调用useBean问题
- jsp与tomcat5.0配置问题 在下愿高分奉上
- 关于Spring依赖注入的问题,求各位大神解答。
- 数据库执行(sql)放入变量中的值,再一条(sql)后怎么就看不到了呢?????
- 想系统学习java和xml结合的编程,请推荐几本好书。
- System.out的问题
- TomCat4.1.3端口绑定问题
- 刚学JSP写了个hellworld.jsp的文件,在IE下运行却是一片空白,还能看到源码?
- JB7+WB7发布SERVLET出问题,怎么搞啊!
- webwork官网地址多少 哪里有webwork学习资料
- Hibernate的离线查询问题,求解答!
一般建database和建表语句不写在后台的。
url写的是jdbc:mysql://localhost:3306/bookstore
不写到后台那是直接写死到Servlet中?