jsp连接MySQL数据库testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
Conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs_db?useUnicode=true&characterEncoding=ISO-8859-1","root","shinco");//连接数据库
Stmt =Conn.createStatement(); //创建语句对象
//获取记录总数RowCount
sSql2 = "select count(*) from t_bbs where istopic=0 ";
Rst=Stmt.executeQuery(sSql2);
Rst.next();
怎么不行呀,楼上的,(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);有什么用的
这条语句是可以让数据指针任意滚动。需要jdbc2.0以上版本才支持。你向mysql中写入数据时new String(s.getBytes("ISO8859_1"),"gb2312");
转换一下,读取数据时就不用转换了。
记得在jsp页面中要加上
<%@ page contentType="text/html;charset=gb2312"%>
我都做了我噢!!就是有上面的问题呀,不支持中文!!
在你写入数据的页面时不要<%@ page contentType="text/html;charset=gb2312"%>
就可以了
你用WIN2K系统->运行->cmd->操作SQL 在DOS下得到的记录也都正常显示了,主要是JAVA本身的问题。
就因为中文问题,所以项目一直都没进展,痛苦ing
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=GBK"
注意后面的编码一定要用GBK,因为gb2312的字库已经不够用了,像朱镕基的镕字。