很急,哪位大侠帮助把下面连数据库的部分改为SQLSERVER和ACCESS的两种代码的?谢谢 !

[复制链接]
查看11 | 回复3 | 2005-2-5 09:52:00 | 显示全部楼层 |阅读模式
文件名:OrderStatusServlet.java,这是个服务器端的程序,另外我怎么在服务器端运行它呀?

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class OrderStatusServlet extends HttpServlet {
static final String dbURL =
"jdbc:mysql://localhost/j2mebook?" +
"user=j2meapps&password=bigsecret";
public void doGet(HttpServletRequest request,

HttpServletResponse response)
throws IOException, ServletException {

Connection conn = null;
String nextJSP = null;
try {
Class.forName("org.gjt.mm.mysql.Driver&quot

;
}
catch (ClassNotFoundException e) {
throw new ServletException("Unable to load JDBC driver&quot

;
}
try {
String orderNumber = request.getParameter("orderNumber&quot

;
conn = DriverManager.getConnection(dbURL);
Statement stmt = conn.createStatement();
String query = "SELECT status, dateplaced, shipdate " +

"FROM orders " + "WHERE ordernumber = '" +

orderNumber + "'";
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
request.setAttribute("orderNumber", orderNumber);
request.setAttribute ("status", rs.getString(1));
request.setAttribute ("datePlaced", rs.getString(2));
String shipDate = rs.getString(3);
if (shipDate == null) {

shipDate = "***";
}
request.setAttribute ("shipDate", shipDate);
nextJSP = "/ReportOrderStatus.jsp";
}
else {
nextJSP = "/OrderNotFound.jsp";
}
conn.close();
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
RequestDispatcher rd = context.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
catch (SQLException e) {
throw new ServletException("SQL call failed&quot

;
}
catch (Exception e) {
throw new ServletException(e.getMessage());
}
finally {
if (conn != null) {
try {

conn.close();
}
catch (SQLException e) {

throw new ServletException("connection close failed&quot

;
}
}
}
}
}
回复

使用道具 举报

千问 | 2005-2-5 09:52:00 | 显示全部楼层
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class OrderStatusServlet extends HttpServlet {
//mysql的url
static final String dbURL ="jdbc:mysql://localhost/j2mebook?" +
"user=j2meapps&password=bigsecret";
//access的url
static final String aURL = "jdbc

dbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\Data Import\\j2mebook.mdb";
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
Connection conn = null, conn1 = null;
Statement stmt = null;
String query = null;
String nextJSP = null;
try {
Class.forName("org.gjt.mm.mysql.Driver&quot

;
}
catch (ClassNotFoundException e) {
throw new ServletException("Unable to load JDBC driver&quot

;
}
try {
String orderNumber = request.getParameter("orderNumber&quot

;
conn = DriverManager.getConnection(dbURL);
if(conn == null){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver&quot

;

}catch(ClassNotFoundException e){

throw new ServletException("Unable to load JDBC driver&quot

;

}

try{

conn1 = DriverManager.getConnection(aURL, "", "&quot

;

stmt = conn1.createStatement();

query = "select status, dateplaced, shipdate from orders where ordernumber = '"+orderNumber+"'";

}

catch(Exception e){}
}else{

stmt = conn.createStatement();

query = "select status, dateplaced, shipdate from orders where ordernumber = '"+orderNumber+"'";

}





ResultSet rs = stmt.executeQuery(query);

if (rs.next()) {

request.setAttribute("orderNumber", orderNumber);

request.setAttribute ("status", rs.getString(1));

request.setAttribute ("datePlaced", rs.getString(2));

String shipDate = rs.getString(3);

if (shipDate == null) {

shipDate = "***";

}

request.setAttribute ("shipDate", shipDate);

nextJSP = "/ReportOrderStatus.jsp";

}

else {

nextJSP = "/OrderNotFound.jsp";

}

ServletConfig config = getServletConfig();

ServletContext context = config.getServletContext();

RequestDispatcher rd = context.getRequestDispatcher(nextJSP);

rd.forward(request, response);

}catch(Exception e){}
finally {

if (conn != null) {

try {

conn.close();

}

catch (SQLException e) {

throw new ServletException("connection close failed&quot

;

}

}

if (conn1 != null) {

try {

conn1.close();

}

catch (SQLException e) {

throw new ServletException("connection close failed&quot

;

}

}
}
}
}
回复

使用道具 举报

千问 | 2005-2-5 09:52:00 | 显示全部楼层
改是不好改了。
你可以参考一下我的。那你只要替换进去就可以了。
package insertandoutputimage.constants;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
public class Constant {
private static String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
private static String USER="sa";
private static String PASS="";
private static String DriverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static Connection getConnection()
{
Connection con = null;
try {
Class.forName(DriverName).newInstance();
con = DriverManager.getConnection(URL, USER, PASS);
}
catch (SQLException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
catch (IllegalAccessException ex) {
ex.printStackTrace();
}
catch (InstantiationException ex) {
ex.printStackTrace();
}
return con;
}
}
回复

使用道具 举报

千问 | 2005-2-5 09:52:00 | 显示全部楼层
这个文件名是OrderStatusServlet.java, 是服务器端的程序,如果将这个JAVA文件生成程序在服务器端运行,然后客户端运行J2ME查询
客户端程序链接地址:
private static final String URL =
"http://localhost/J2MEBOOK/servlet/OrderStatusServlet" +
"?orderNumber=";
public void getOrderStatus() {
HttpConnection conn = null;
InputStream is = null;
OutputStream os = null;
byte[] receivedData = null;
try {
StringBuffer sb = new StringBuffer(URL);
sb.append(orderNumber.getString());
conn =

(HttpConnection)Connector.open(sb.toString());
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent",

"Profile/MIDP-1.0 Configuration/CLDC-1.0&quot

;
conn.setRequestProperty("Content-type",

"application/x-www-form-urlencoded&quot

;
is = conn.openInputStream();
String contentType = conn.getType();
int len = (int)conn.getLength();
if (len > 0) {

receivedData = new byte[len];

int nb = is.read(receivedData);
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行