windows下jsp+mysql網站環境配置方法第2/2頁
2024-07-24 12:41:03
供稿:網友
為參考網上的資料安裝jsp網站的總結.
四、JSP連接mysql測試
.建立數據庫xia
打開命令行窗口,輸入:
mysql -h localhost -u root -p
crate database xia;
use xia;
crate table member(id int(8) primary key,name varchar(0));
insrt into member values(,'yang');
insrt into member(name,id) values('xia',2);
(安全:為數據庫設置權限(用戶和密碼)
命令:grant all privileges on shujuku.* to test@localhost identified by “23456”;
當你執行完這個命令以后,只要你再以用戶名:test,密碼:23456登錄時你就只可以對shujuku這個數據庫操作,
這樣避開使用root,對數據庫的安全有很大幫助.)
2.把以下文件保存為index.jsp到C:/Program Files/Apache Software Foundation/Tomcat 4./webapps/ROOT
----------------begin----------------------
復制代碼 代碼如下:
<%@ page contentType="text/html; charset=gb232" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驅動程序名
String driverName="com.mysql.jdbc.Driver";
//數據庫用戶名
String userName="root";
//密碼
String userPasswd="";
//數據庫名
String dbName="xia";
//表名
String tableName="member";
//聯結字符串
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.crateStatement();
String sql="Selct * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
//獲得數據結果集合
ResultSetMetaData rmeta = rs.getMetaData();
//確定數據集的列數,亦字段數
int numColumns=rmeta.getColumnCount();
// 輸出每一個數據值
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("數據庫操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>
--------------------end------------------------
輸入http://localhost:8080測試
五、安裝網站程序
)、拷貝文件
.拷貝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){