一、安裝
默認系統已正確安裝Myeclipse,且正確配置Tomcat+JDK,該文檔基於Myeclipse版本爲Myeclipse8.5、Tomcat6.0、JDK1.7
安裝MySQL5.7+Win10_64
1)下載解壓版mysql-5.7.14-winx64
解壓到C:\Program Files\MySQL\mysql-5.7.14-winx64路徑下,解壓到其他盤在win10新電腦上會出現問題,所以建議解壓到C盤下。
2)配置系統變量
Path中追加C:\ProgramFiles\MySQL\mysql-5.7.14-winx64\bin
3) 其他參考
原文:http://jingyan.baidu.com/article/af9f5a2d16fa4d43150a4552.html或轉載:http://blog.csdn.net/micro_ryan/article/details/54629220
4)若出現MySql服務正在啓動,,,服務無法啓動的問題
參考原文:http://blog.csdn.net/u013067166/article/details/49951577或轉載:http://blog.csdn.net/micro_ryan/article/details/54629293
二、cmd啓動mysql創建數據
1)進入mysql安裝路徑的bin路徑下
>cd C:\Program Files\MySQL\mysql-5.7.14-winx64\bin
>net start mysql 啓動mysql
>mysql –u root –p 登陸mysql
沒有密碼,初次登陸沒有密碼
--修改root的密碼,將密碼設置爲a1234
>set password for ‘root’@’localhost’=password(‘a1234’);
2)創建數據庫及數據表
--創建數據庫,數據庫名爲cesec_ITS
Create database cesec_ITS;
--爲數據庫分配權限及密碼
Grant select,insert,update,delete,create,drop,alter on cesec_ITS.* to cesec_ITS@localhost Identified by ‘tiger’;
--修改密碼
//Set password for ‘cesec_ITS’@’cesec_ITS’= OLD_PASSWORD(‘密碼’);
3)創建數據表
--進入數據庫,指定數據表存放在哪個數據庫
use 數據庫名;
>use cesec_ITS;
--創建數據表,數據表名爲card
Create table 數據表名(字段1 屬性,字段2 屬性,····);
>Create table card (id INT,usernameVARCHAR(20),password VARCHAR(20),age INT,weight INT,sex VARCHAR(20));
--將數據插入數據表
Insert into 數據表名(字段1,字段2,···) values(字段1的值,字段2的值);
>Insert intocard(id,username,password,age,weight,sex) values(1,’Ryan’,’tiger’,27,140,’M’);
三、安裝mysql可視化工具及JDBC驅動
3.1 Mysql Browser
登陸界面如下
如果修改root的密碼則填寫密碼,若沒有則直接點擊ok,在接下來的窗口中點擊Ignore進入mysql管理主界面。
查看剛剛新建的數據庫及數據表
3.2下載java與mysql連接的驅動JDBC
解壓即可
四、myeclipse中配置mysql數據庫
4.1 新建web project工程
在webroot中新建login.jsp工程
編寫登陸界面如下
4.2在src中創建Login包
在Login包下創建LoginTestServlet.java
即創建Servlet容器,用於響應web客戶端發出的http請求
HttpServlet容器響應Web客戶請求流程如下:
1)Web客戶向Servlet容器發出Http請求;
2)Servlet容器解析Web客戶的Http請求;
3)Servlet容器創建一個HttpRequest對象,在這個對象中封裝Http請求信息;
4)Servlet容器創建一個HttpResponse對象;
5)Servlet容器調用HttpServlet的service方法,把HttpRequest和HttpResponse對象作爲service方法的參數傳給HttpServlet對象;
6)HttpServlet調用HttpRequest的有關方法,獲取HTTP請求信息;
7)HttpServlet調用HttpResponse的有關方法,生成響應數據;
8)Servlet容器把HttpServlet的響應結果傳給Web客戶。
創建HttpServlet的步驟——“四部曲”
1)擴展HttpServlet抽象類;
2)覆蓋HttpServlet的部分方法,如覆蓋doGet()或doPost()方法;
3)獲取HTTP請求信息。通過HttpServletRequest對象來檢索HTML表單所提交的數據或URL上的查詢字符串;
4)生成HTTP響應結果。通過HttpServletResponse對象生成響應結果,它有一個getWriter()方法,該方法返回一個PrintWriter對象。
4.3 代碼中部分註釋
/*重寫/覆蓋doPost()方法,其中request變量爲獲取登錄網頁的控件值,response變量爲返回網頁的值*/
protectedvoid doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { }
/*獲取登錄網頁的username及password的值*/
String username = request.getParameter("username");
String psw =request.getParameter("password");
/*創建於數據庫連接的連接字符*/
String url = "jdbc:mysql://localhost:3306/cesec_ITS";
Connectionconn = null;
Statement stmt= null;
ResultSet rs =null;
/*連接數據庫,綁定具體數據庫及密碼*/
conn =(Connection) DriverManager.getConnection(url,"root","a1234");
/*創建mysql語句執行體*/
stmt =(Statement) conn.createStatement();
/*創建SQL語句*/
String sql = "select *from card where username='"+username+"' and password= '"+psw+"'";
/*執行SQL語句,返回執行SQL語句的數據表*/
rs =stmt.executeQuery(sql);
/*判斷有無可匹配的數據*/
rs.next()
/*新建能夠保存返回值的變量*/
HttpSessionsession = request.getSession();
/*保存從數據表讀取的字段值*/
ession.setAttribute("username", username);
session.setAttribute("age",rs.getString("age")) ;
session.setAttribute("sex",rs.getString("sex")) ;
session.setAttribute("weight",rs.getString("weight"));
/*跳轉到指定頁面*/
response.sendRedirect("success.jsp") ;
4.4編寫其他網頁及配置web.xml文件
其他跳轉網頁如下
Web.xml部署文件工程的Servlet容器
或在Source中填寫代碼如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginTestServlet</servlet-name>
<servlet-class>Login.LoginTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginTestServlet</servlet-name>
<url-pattern>/LoginTestServlet</url-pattern>
</servlet-mapping>
</web-app>
4.5給項目配置數據庫
右鍵項目名,進入項目屬性,進入如下圖界面
點擊”Add External JARs…”,選擇到JDBC的路徑下,添加如下
選擇之後點擊OK
4.6編輯工程運行