下面是一个登陆数据库的页面 它调用了一个sqlbean.java 我应该怎样修改才把它们两个放到一个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>
下面是那个sqlbean.java
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("org.gjt.mm.mysql.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/wsxk", "root", "1234567"));
} 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;
}
}
<%@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>
下面是那个sqlbean.java
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("org.gjt.mm.mysql.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/wsxk", "root", "1234567"));
} 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;
}
}
解决方案 »
- 如果我用java在某个目录不停的生成一些目录和文件。同时有
- 使用jstl的out标签出错的问题
- 关于损失精度的提示
- java小问题
- 我用Java做的MSN机器人([email protected])加我有分啊
- UltraEdit的“搜索”—“查找”结果怎么老是出错?
- 跪求一个关于字符匹串配的算法问题
- 听说 Java 2 核心技术 卷II:高级性能要出第6版了?
- 已知a=1,b=2 String string="a+b";如何将string转化为表达式,并计算结果
- 在登录时,系统会提示你是否保存密码,请问怎么将此提示去掉
- 2008年应届生求职java相关工作
- 下面的代码编译不动~~~ 代码有没问题啊? 嗷嗷!!!!!!!
因为那个sqlbean中没有main函数 它就不能单独编译过 我就是想把把的代码放到logincheck的页面中这样就可以不用调用sqlbean了把
或者还有什么方法能让sqlbean.java能单独编译成功的方法吧 我现在不清楚 书上也没有