聊天室jsp代码

jiappkpp 2008-03-20 10:38:05
我在调节一个聊天室代码的页面呀 它是通过logincheck.jsp连接的sqlsever2000的数据库 而我想通过修改用它连接mysql数据库应该这么改呀 顺便介绍一些修改jsp代码的知识哈
Logincheck.jsp
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="sqlbean" scope="page" class="database.sqlbean">
</jsp:useBean>
<html>
<head>
<title>登录检查</title>
</head>
<body>

<%

String userType = request.getParameter("userType");


String regName=sqlbean.AsciiToChineseString(request.getParameter("username").trim());
String regPassword=request.getParameter("password");
regPassword=regPassword.trim();
session.setAttribute("originname",regName);

if (userType.equals("Y"))
{
ResultSet rs=null;
String sqlstring = "select * from chatuser where username='"+ regName +"' and password='"+ regPassword + "'";;
rs = sqlbean.executeQuery(sqlstring);
if(rs.next())
{

String experience=rs.getString("experience");
String userlevel=rs.getString("userlevel");
session.setAttribute("experience",experience);
session.setAttribute("userlevel",userlevel);

regName = regName + "[会员]";

session.setAttribute("username",regName);

rs.close();
sqlbean.freeRs(rs);
response.sendRedirect ("chat.jsp");
}
else
{
rs.close();
out.print("<script>alert(\"用户名/密码错误!\");");
out.print("window.location = \"login.jsp\"</script>");
}
}
else
{
if((regName.length()<1)||(regName.length()>10)) {
out.print("<script>alert(\"游客名不能超过6个字长!\");window.close();</script>");
return;
}

Character c=new Character(' ');
for(int i=0;i<regName.length();i++) {
if(regName.charAt(i) == c.charValue()) {
out.print("<script>alert(\"名字中间不能有空格!\");window.close();</script>");
return;
}
}
regName = regName + "[非会员]";
}

synchronized (application)
{
Vector UserName=null;
UserName= (Vector)application.getAttribute("UserName");
if(UserName==null) {
UserName= new Vector(30,10);
}

if(UserName.contains(regName)) {
out.print("<script>alert(\"你的名字正在被人使用!\");window.close();</script>");
return;
}

if(regName!=null ) {
UserName.addElement(regName);
session.putValue("username", regName);
}


application.setAttribute("UserName",UserName);
out.print("<script>window.location=\"chat.jsp\"</script>");
}
%>
</body>
</html>
在这个里面它调用了javabean
package database;

import java.io.PrintStream;
import java.sql.*;
import sun.io.ByteToCharConverter;

public class sqlbean
{

private Connection con;
private ResultSet rs;

public sqlbean()
{
}

public static Connection getConnection()
throws SQLException
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=chatdb", "sa", "");
}

public ResultSet executeQuery(String sql)
{
try
{
con = getConnection();
Statement statement = con.createStatement();
rs = statement.executeQuery(sql);
}
catch(SQLException sqlexception) { }
return rs;
}

public int executeUpdate(String sql)
{
int count = 0;
Statement stmt = null;
try
{
con = getConnection();
stmt = con.createStatement();
count = stmt.executeUpdate(sql);
}
catch(SQLException sqlexception) { }
finally
{
try
{
if(stmt != null)
{
stmt.close();
}
if(con != null)
{
con.close();
}
}
catch(SQLException ex)
{
System.err.print(ex);
}
}
return count;
}

public void freeRs(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
con.close();
}
}
catch(Exception exception) { }
}

public static String AsciiToChineseString(String s)
{
char orig[] = s.toCharArray();
byte dest[] = new byte[orig.length];
System.out.println("111111");
System.out.println(s);
for(int i = 0; i < orig.length; i++)
{
dest[i] = (byte)(orig[i] & 0xff);
}

try
{
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}
catch(Exception exception)
{
System.out.println("222222222");
}
System.out.println(s);
return s;
}
}
...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuwenbao 2008-03-20
  • 打赏
  • 举报
回复
sqlbeen中

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
改成Class.forName("org.gjt.mm.mysql.Driver");

DriverManager.getConnection("jdbc:mysql://localhost:3306/你的数据库名", "你的用户名", "你的密码");
winer2008 2008-03-20
  • 打赏
  • 举报
回复
在sqlbeen中,
这要看你mysql用的驱动,
如果是org.gjt.mm.mysql.Driver的驱动把Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
改成Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"),DriverManager.getConnection("jdbc:mysql://localhost:3306/你的数据库名", "你的用户名", "你的密码")就可以了;
如果是com.mysql.jdbc.Driver的驱动就要改成Class.forName("com.mysql.jdbc.Driver")"),DriverManager.getConnection("jdbc:mysql://localhost:3306/你的数据库名", "你的用户名", "你的密码")就可以了,

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧