以下三个文件是JSP连接MYSQL的文件,怎样能转换成连接SQL SERVER 的文件?相当急~~~JinghuaConn.javapackage jinghua;/**
* <p>Title: Jinghua-TestOnline</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Bit</p>
* @author ChengBaodong
* @version 1.0
*/import java.sql.*;//sqlConn=java.sql.DriverManager.getConnection(
//"jdbc:mysql://localhost/train?user=support&password=support&useUnicode=true&characterEncoding=gb2312");
//String MM_con1_DRIVER = "com.mysql.jdbc.Driver";
//String MM_con1_USERNAME = "root";
//String MM_con1_PASSWORD = "";
//String MM_con1_STRING = "jdbc:mysql://localhost/jinghua";
//Driver DriverRecordset1 = (Driver)Class.forName(MM_con1_DRIVER).newInstance();
//Connection Con = java.sql.DriverManager.getConnection(MM_con1_STRING,MM_con1_USERNAME,MM_con1_PASSWORD);public class JinghuaConn {
private static String rootpath="D:\\JavaApp\\Jinghua\\test_online";
private static String chartPath="D:\\JavaApp\\Jinghua\\test_online\\chart\\temp";
// private static String rootpath="/sfroot/home/groups/jsptestonline";
// private static String chartPath="/sfroot/home/groups/jsptestonline/chart/temp";
private String sample = "Sample";
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String HOST = "http://localhost:8080";// private static final String HOST = "http://jsptestonline.cosoft.org.cn";
private static final String PHYPATH = "D:/JavaApp/Jinghua/test_online";
// private static final String PHYPATH = "/sfroot/home/groups/jsptestonline";
private static final String CONNECTION_STRING =
"jdbc:mysql://localhost/jinghua";
public static String getRootPath(){
return rootpath;
}
public static String getChartPath(){
return chartPath;
} public JinghuaConn() {
try {
Driver DriverJinghua = (Driver) Class.forName(DRIVER).newInstance();
//Con = DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
}
catch (ClassNotFoundException e) {
System.err.println("JinghuaConn():" + e.toString());
}
catch (Exception e) {
System.err.println("JinghuaConn():" + e.toString());
} } public Connection getConnection() {
try { //Con = DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
String strUrl = CONNECTION_STRING + "?user=" + USERNAME +
"&password=" +
PASSWORD + "&useUnicode=true&characterEncoding=gb2312";
con = DriverManager.getConnection(strUrl); } catch (Exception e) {
con = null;
} return con;
} public void dropConnection() {
try {
closeStmt();
con.close();
}
catch (Exception ignored) {}
finally {
con = null;
}
} public ResultSet executeQuery(String sql) {
rs = null;
try {
//conStr = conStr + user ;
con = getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("JinghuaConn.executeQuery(): " + ex.getMessage() );
}
return rs;
} public void executeUpdate(String sql) {
stmt = null;
rs = null;
try {
//conStr = conStr + user ;
con =getConnection();
stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("JinghuaConn:executeUpdate(): " + ex.getMessage());
}
} public void execute(String sql) {
stmt = null;
rs = null;
try {
//conStr = conStr + user ;
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("JinghuaConn:execute(): " + ex.getMessage());
}
} public void closeStmt() {
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
} public void closeConn() {
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
* <p>Title: Jinghua-TestOnline</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Bit</p>
* @author ChengBaodong
* @version 1.0
*/import java.sql.*;//sqlConn=java.sql.DriverManager.getConnection(
//"jdbc:mysql://localhost/train?user=support&password=support&useUnicode=true&characterEncoding=gb2312");
//String MM_con1_DRIVER = "com.mysql.jdbc.Driver";
//String MM_con1_USERNAME = "root";
//String MM_con1_PASSWORD = "";
//String MM_con1_STRING = "jdbc:mysql://localhost/jinghua";
//Driver DriverRecordset1 = (Driver)Class.forName(MM_con1_DRIVER).newInstance();
//Connection Con = java.sql.DriverManager.getConnection(MM_con1_STRING,MM_con1_USERNAME,MM_con1_PASSWORD);public class JinghuaConn {
private static String rootpath="D:\\JavaApp\\Jinghua\\test_online";
private static String chartPath="D:\\JavaApp\\Jinghua\\test_online\\chart\\temp";
// private static String rootpath="/sfroot/home/groups/jsptestonline";
// private static String chartPath="/sfroot/home/groups/jsptestonline/chart/temp";
private String sample = "Sample";
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String HOST = "http://localhost:8080";// private static final String HOST = "http://jsptestonline.cosoft.org.cn";
private static final String PHYPATH = "D:/JavaApp/Jinghua/test_online";
// private static final String PHYPATH = "/sfroot/home/groups/jsptestonline";
private static final String CONNECTION_STRING =
"jdbc:mysql://localhost/jinghua";
public static String getRootPath(){
return rootpath;
}
public static String getChartPath(){
return chartPath;
} public JinghuaConn() {
try {
Driver DriverJinghua = (Driver) Class.forName(DRIVER).newInstance();
//Con = DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
}
catch (ClassNotFoundException e) {
System.err.println("JinghuaConn():" + e.toString());
}
catch (Exception e) {
System.err.println("JinghuaConn():" + e.toString());
} } public Connection getConnection() {
try { //Con = DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
String strUrl = CONNECTION_STRING + "?user=" + USERNAME +
"&password=" +
PASSWORD + "&useUnicode=true&characterEncoding=gb2312";
con = DriverManager.getConnection(strUrl); } catch (Exception e) {
con = null;
} return con;
} public void dropConnection() {
try {
closeStmt();
con.close();
}
catch (Exception ignored) {}
finally {
con = null;
}
} public ResultSet executeQuery(String sql) {
rs = null;
try {
//conStr = conStr + user ;
con = getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("JinghuaConn.executeQuery(): " + ex.getMessage() );
}
return rs;
} public void executeUpdate(String sql) {
stmt = null;
rs = null;
try {
//conStr = conStr + user ;
con =getConnection();
stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("JinghuaConn:executeUpdate(): " + ex.getMessage());
}
} public void execute(String sql) {
stmt = null;
rs = null;
try {
//conStr = conStr + user ;
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("JinghuaConn:execute(): " + ex.getMessage());
}
} public void closeStmt() {
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
} public void closeConn() {
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
解决方案 »
- 查询问题
- 绝对疑难杂症,高分请专家进!
- 菜鸟tomcat+sqlserver2000连不上数据库,求教高手!
- 我用jstl,前面的两个问题没有了,但是又有新问题了。大家都来看看
- 一个关于jsp数据库操作的问题!!急急!!!!!!
- 请问这两个包在哪里?import javax.servlet.*;import javax.servlet.http.*;
- JSP 有什么好的开发环境吗?
- Applet向servlet提交时,如果是中文字符串时,出现乱骂,该如何?
- tomcat手动启动web项目,配置文件路径怎么改?
- 百度地图加载谷歌瓦片
- 在java里添加数据到xml里怎么弄?请教各位了!
- 我定义了一个类型:StringBuffer sBuf= new StringBuffer();我该如何清空sBuf里的内容呢?
import java.sql.*;//==========================================
// Define Class DBConn
//==========================================
public class DBConn
{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:jinghuaOdbc";
public String user = "sa";
public String Passwd = ""; Connection conn = null;
Statement stmt = null;
ResultSet rs = null; public boolean setUser(String user)
{
this.user = user;
return true;
} public String getUser()
{
return this.user;
} public boolean setPwd(String Passwd)
{
this.Passwd = Passwd;
return true;
} public String getPwd()
{
return this.Passwd;
} public dbConn()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
} //执行查询操作
public ResultSet executeQuery(String strSql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
} //执行数据的插入、删除、修改操作
public boolean execute(String strSql)
{
try{
if(this.stmt.executeUpdate(strSql) == 0)
return false;
else
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}catch(NullPointerException e){
System.out.println(e.toString());
return false;
}
} //结果集指针跳转到某一行
public boolean rs_absolute(int row)
{
try{
this.rs.absolute(row);
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
} public void rs_afterLast()
{
try{
this.rs.afterLast();
}catch(SQLException e){
System.out.println(e.toString());
}
} public void rs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLException e){
System.out.print(e.toString());
}
} public void rs_close()
{
try{
this.rs.close();
}catch(SQLException e){
System.out.print(e.toString());
}
} public void rs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLException e){
System.out.print(e.toString());
}
} public boolean rs_first()
{
try{
this.rs.first();
return true;
}catch(SQLException e){
System.out.print(e.toString());
return false;
}
} public String rs_getString(String column)
{
try{
return this.rs.getString(column);
}catch(SQLException e){
System.out.println(e.toString());
return null;
}
} //此方法用于获取大段文本,
//将其中的回车换行替换为<br>
//输出到html页面
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "\r\n";
String str3 = "<br>";
return this.replaceAll(str1,str2,str3);
}catch(SQLException e){
System.out.println(e.toString());
return null;
}
}
//把str1字符串中的str2字符串替换为str3字符串
private static String replaceAll(String str1,String str2,String str3)
{
StringBuffer strBuf = new StringBuffer(str1);
int index=0;
while(str1.indexOf(str2,index)!=-1)
{
index=str1.indexOf(str2,index);
strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
index=index+str3.length(); str1=strBuf.toString();
}
return strBuf.toString();
} public int rs_getInt(String column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e){
System.out.println(e.toString());
return -1;
}
} public int rs_getInt(int column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e){
System.out.println(e.toString());
return -1;
}
} public boolean rs_next()
{
try{
return this.rs.next();
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
} //判断结果集中是否有数据
public boolean hasData()
{
try{
boolean has_Data = this.rs.first();
this.rs.beforeFirst();
return has_Data;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
} public boolean rs_last()
{
try{
return this.rs.last();
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
} public boolean rs_previous()
{
try{
return this.rs.previous();
}catch(Exception e){
System.out.println(e.toString());
return false;
}
} //main方法,调试用
public static void main(String args[])
{
try{
DBConn myconn = new DBConn();
//myconn.setDBName("shopping");
//myconn.DBConn();
//myconn.execute("Insert Into tbl_test(ID,UserID) values('10','shandaer')");
//myconn.execute("Update tbl_test set UserID='yyyyyyyyyyyy' where ID=10");
//myconn.execute("Delete from tbl_test where ID=1");
ResultSet rs = myconn.executeQuery("select * from sysadmin order by ID desc limit 1");
//boolean hasData = myconn.hasData();
//System.out.println("has data:" + hasData);
//rs.first();
while (myconn.rs.next())
{
int ID = myconn.rs_getInt("ID") + 1;
System.out.print(ID);
System.out.println(myconn.rs_getInt("ID") + myconn.rs_getString("UserID"));
//System.out.println('\n' + myconn.rs_getHtmlString("UserID"));
//System.out.println(myconn.rs.getString("UserID") + myconn.rs_getInt(1));
}
}catch(Exception e){
System.err.println(e.toString());
}
}
}
<%@ page import="jinghua.CheckUser" %>
<%@ page import="jinghua.*" %>
<%!//处理中文字符串
public String codestring(String s){
String str=s;
try{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}
catch(Exception e){
return str;
}
}
%><jsp:useBean id="login" class="jinghua.CheckUser" scope="session">
</jsp:useBean>
<%//提交信息后,验证是否正确
String message="",
logrole="",
logname="",
password="";
//if(!(session.isNew())){
logrole=request.getParameter("logrole");
if(logrole==null) logrole="";
logrole=codestring(logrole); logname=request.getParameter("logname");
if(logname==null) logname="";
logname=codestring(logname); password=request.getParameter("password");
if(password==null) password="";
password=codestring(password);
//}
%><%
if(!(logname.equals(""))){
%>
<jsp:setProperty name="login" property="logrole" value="<%=logrole%>"/>
<jsp:setProperty name="login" property="logname" value="<%=logname%>"/>
<jsp:setProperty name="login" property="password" value="<%=password%>"/><%
message=login.getMessage();//获取返回的验证信息
if (message==null) message="";
}
%>
<%
session.setMaxInactiveInterval(60*60);
//Tools.debugPrintln("session.setMaxInactiveInterval(60*60*2))");
if(!(session.isNew())){
if(message.equals("OK")) { if(logrole.equals("teacher")){ session.setAttribute("teacherID",login.getID() );
//String questions=response.encodeUrl();
response.sendRedirect("teacher/index-frames.jsp");
}
else if(logrole.equals("student")){ session.setAttribute("studentID",login.getID());
//String questions=response.encodeUrl();
response.sendRedirect("student/stu_test.jsp");
}
else if(logrole.equals("admin")){ session.setAttribute("adminID",login.getID() );
//String questions=response.encodeUrl();
response.sendRedirect("administrator/admin.htm"); } }
else{
out.print(message); //String loginpage=response.encodeUrl("login.jsp");
//response.sendRedirect(loginpage);
}
}
%>
<html>
<head>
<title>
CheckUser
</title>
</head>
<body bgcolor="#ffffff"><SCRIPT language="javascript">
alert("用户名或密码错误,请重新登录");
window.location = "login.jsp";
</SCRIPT>
<h1></h1>
</body>
</html>
系统登陆的时候首先检测CheckUser.jsp文件