欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

windows下jsp+mysql網(wǎng)站環(huán)境配置方法

 更新時間:2008年09月18日 10:11:30   作者:  
為參考網(wǎng)上的資料安裝jsp網(wǎng)站的總結(jié).

四、JSP連接mysql測試
.建立數(shù)據(jù)庫xia
打開命令行窗口,輸入:
mysql -h localhost -u root -p
cr�ate database xia;
use xia;
cr�ate table member(id int(8) primary key,name varchar(0));
ins�rt into member values(,'yang');
ins�rt into member(name,id) values('xia',2);
(安全:為數(shù)據(jù)庫設(shè)置權(quán)限(用戶和密碼)
命令:grant all privileges on shujuku.* to test@localhost identified by “23456”;
當(dāng)你執(zhí)行完這個命令以后,只要你再以用戶名:test,密碼:23456登錄時你就只可以對shujuku這個數(shù)據(jù)庫操作,
這樣避開使用root,對數(shù)據(jù)庫的安全有很大幫助.)
2.把以下文件保存為index.jsp到C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT
----------------begin----------------------
復(fù)制代碼 代碼如下:

<%@ page contentType="text/html; charset=gb232" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驅(qū)動程序名
String driverName="com.mysql.jdbc.Driver";
//數(shù)據(jù)庫用戶名
String userName="root";
//密碼
String userPasswd="";
//數(shù)據(jù)庫名
String dbName="xia";
//表名
String tableName="member";
//聯(lián)結(jié)字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.cr&#0;ateStatement();
String sql="Sel&#0;ct * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
//獲得數(shù)據(jù)結(jié)果集合
ResultSetMetaData rmeta = rs.getMetaData();
//確定數(shù)據(jù)集的列數(shù),亦字段數(shù)
int numColumns=rmeta.getColumnCount();
// 輸出每一個數(shù)據(jù)值
out.print("id");
out.print("|");
out.print("name");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString()+" ");
out.print("|");
out.print(rs.getString(2));
out.print("<br>");
}
out.print("<br>");
out.print("數(shù)據(jù)庫操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>

--------------------end------------------------
輸入http://localhost:8080測試
五、安裝網(wǎng)站程序
)、拷貝文件
.拷貝C:\Program Files\MySQL\MySQL Server 5.0\data\dataname
2.把
C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT\mywebroot
C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT\web-inf
目錄拷貝過去
3.建立
C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT\web-inf\classes
把用到的包拷貝到
C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT\web-inf\classes
目錄下面
2)、javabean安裝配置測試
建立自己的Bean:
.文件名TestBean.java:
--------begin---------
package test;
public class TestBean{
private String name = null;
public TestBean(String strName_p){
this.name=strName_p;
}
public void setName(String strName_p){
this.name=strName_p;
}
public String getName(){
return this.name;
}
}
---------end-------------
2 .編譯
將TestBean.java放在c:\test下,使用如下命令編譯:
C:\Test>javac TestBean.java
然后在c:\Test下會產(chǎn)生一個編譯后的bean文件:TestBean.class
3 .將TestBean.class文件剪切到 C:\Program Files\Apache Software Foundation\Tomcat 4.\webapps\ROOT\WEB-INF\classes\test
4 .新建一個TestBean.jsp文件,文件內(nèi)容為:
<%@ page import="test.TestBean" %>
<html><body><center>
<%
TestBean testBean=new TestBean("This is a test java bean.");
%>
Java bean name is: <%=testBean.getName()%>
</center></body></html>
5 .好了,重啟Tomcat,啟動瀏覽器,輸入http://localhost:8080/TestBean.jsp
如果看到輸出Java bean name is: This is a test java bean.就說明編寫的Bean成功了。
六、問題解決
.頁面亂碼
在jsp文件前面加
<%@page contentType="text/html; charset=gb232"%>
2.mysql返回結(jié)果亂碼
解決方法一:
連接mysql時(無論在從mysql讀還是取數(shù)據(jù)的情況),指定使用的編碼方式為gb232,具體代碼如下
//裝載mysql-jdbc驅(qū)動
Class.forName("com.mysql.jdbc.Driver").newInstance();
//連接數(shù)據(jù)庫
Connection sqlCon = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gb232" );
解決方法二:
如果方法一不行那么在方法一的基礎(chǔ)上對讀入的字符串進行強制編碼方式轉(zhuǎn)換。
代碼示例如下:
String name = rst.getString("name");
name= new String(name.getBytes("ISO-8859-"),"gb232");
注:代碼也可以為:String name =new String( rst.getString("name").getBytes("ISO-8859-"),"gb232"));其中rst為返回的resultset,ISO-8859-為mysql默認的編碼方式,代碼的目的是把以ISO-8859-的編碼轉(zhuǎn)換為gb232編碼方式,這樣強制轉(zhuǎn)換,可以解決一部分問題,如果結(jié)合方法一,應(yīng)該可以解決中文亂碼問題。

相關(guān)文章

最新評論