我想把这程序的数据库改成SQLSERVER的.. .麻烦帮帮忙.. .package 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=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=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;
}
}
}
}
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=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=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;
}
}
}
}
解决方案 »
- doubleselect怎么查不到东西
- 关于spring+hibernate整合问题 高分求救 不够再加
- 问个入门问题:关于form post中文的问题
- 急,为什么非得包文件复制到webapps\examples\jsp\目录下JSP才能运行.......
- 在myeclipse中怎么把web项目和ejb工程一起调试
- 请问这两个包在哪里?import javax.servlet.*;import javax.servlet.http.*;
- 非常怪的问题:两次myResultSet.getString("name")就出错!!为啥啊??
- hibernate自动建表失败
- 跪求spring 集成 quartz 如何动态添加新job,修改job时间的例子
- struts2 上传文件
- 关于applet内存益处问题!
- 安装sqlserver2000时,提示:以前的某个程序已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机。
url改爲:jdbc:microsoft:sqlserver://host:1433;DatabaseName=XXX
還有user和password
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=getInitParameter("driverClass");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs","sa",""); }
catch(SQLException e){
e.printStackTrace();
}
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;
}
}
}
}
2、下好SQL Server JDBC驱动安装程序,并安装(自动安装在
Program Files\Microsoft SQL Server 2000 Driver for JDBC)
3、把SQL Server JDBC驱动包
Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib
下三个.jar文件复制到WEB-INF\lib中
4、修改web.xml文件把CreateDBServlet的四个初始化参数改一下
driverClass com.microsoft.jdbc.sqlserver.SQLServerDriver
url jdbc:microsoft:sqlserver://localhost:1433;databasename=你的库名
user 用户名
password 口令
这段估计要改一下,MYSQL和MSSQL的字段设置有些不一样。
程序第二次运行时肯定会报错,因为在第一次运行时已经建了数据库和表,第二次运行时就会冲突。