<%@ page language="java" import="java.util.*" import="com.mysql.jdbc.Driver" import="java.sql.*" contentType="text/html;charset=gb2312" pageEncoding="UTF-8"%> <!--匯入相關的類 ,規定編碼gb2312--> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'creatStuTable.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <% Connection conn; Statement stat; //取得連線的url String url="jdbc:mysql://localhost:3306/student"; //使用使用者名稱root,因為在建立資料庫時沒有指定資料庫的使用者名稱,故使用安裝mysql配置的root,密碼也是安裝時我配置的zhangweijie. String userName="root"; String password="zhangweijie"; try{ //註冊JDBC驅動程式 Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException ex) { out.println("找不到驅動程式!"); } //開啟資料庫連線 conn=DriverManager.getConnection(url,userName,password); try{ stat=conn.createStatement(); //如果存在同名的資料表,先進行刪除 stat.executeUpdate("drop table if exists Student_Info;"); //執行建立表的SQL語句 stat.executeUpdate("create table Student_Info (StuID int not null auto_increment,StuName varchar(50) not null,Telephone varchar(20) not null,primary key(StuID));"); //列印提示結果 out.println("資料表Student_Info建立成功"); } catch(SQLException ex) { out.println("資料表Student_Info建立失敗"); } finally{ //關閉資料庫連線 conn.close(); } %> </body> </html>