以下是我自己想到的實現(xiàn)方法檀葛,如果讀者有更好的方法實現(xiàn),懇請指點靡馁。
指定.gif
數(shù)據(jù)表.gif
1.簡單的jsp表單
index.jsp
獲取希望創(chuàng)建的數(shù)據(jù)表名稱和列數(shù)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>動態(tài)創(chuàng)建數(shù)據(jù)表</title>
<meta charset="utf-8">
</head>
<body>
<form action="createTableSlt" method="post">
表名<input type="text" name="tablename" /><br>
列數(shù)<input type="number" name="length" /> <input type="submit" value="提交" />
</form>
</body>
</html>
2.servlet
2.1新建createTableSlt.java文件
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.Create;
public class createTableSlt extends HttpServlet {
public createTableSlt() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* 獲取傳遞來的數(shù)據(jù)表名和列數(shù)欲鹏,調用創(chuàng)建數(shù)據(jù)表的方法
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//以下是關鍵代碼
String tablename = request.getParameter("tablename");
int length = Integer.valueOf(request.getParameter("length"));
Create.createTable(tablename, length);
out.print("<!DOCTYPE html><html><body>成功</body></html>");
}
public void init() throws ServletException {
}
}
2.2修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<servlet-name>createTableSlt</servlet-name>
<servlet-class>servlet.createTableSlt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>createTableSlt</servlet-name>
<url-pattern>/createTableSlt</url-pattern>
</servlet-mapping>
</web-app>
3.為了省事,bean與連接數(shù)據(jù)庫合并在一個類里寫了
package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Create {
static String user = "root";
static String pw = "123456";
//數(shù)據(jù)庫名為tables
static String url = "jdbc:mysql://localhost:3306/tables";
/**
* 創(chuàng)建連接
*
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, pw);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 關閉連接
*
* @param connection
*/
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 創(chuàng)建指定數(shù)據(jù)表名和屬性總數(shù)的數(shù)據(jù)表
*
* @param tablename
* @param length
*/
public static void createTable(String tablename, int length) {
Connection connection = getConnection();
String sql = sql(tablename, length);
try {
PreparedStatement psStatement = (PreparedStatement) connection
.prepareStatement(sql);
psStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 動態(tài)生成sql建表語句臭墨,數(shù)據(jù)表為指定名稱赔嚎、指定屬性長度(屬性類型為String,主鍵為自動增長的id)
*
* @param tablename
* @param length
* @return sql
*/
public static String sql(String tablename, int length) {
StringBuffer sql = new StringBuffer();
sql.append("create table " + tablename + "(");
sql.append("id int(10) not null primary key auto_increment,");
String columns[] = columnsName(length);
for (int i = 0; i < columns.length; i++) {
if (i == columns.length - 1) {
sql.append(columns[i] + " varchar(20) not null");
break;
}
sql.append(columns[i] + " varchar(20) not null,");
}
sql.append(");");
return sql.toString();
}
/**
* 動態(tài)生成形如column_A形式的列名
*
* @param length
* @return
*/
public static String[] columnsName(int length) {
String columnsName[] = new String[length];
for (int i = 0; i < length; i++) {
char a = (char) (65 + i);
columnsName[i] = "columns_" + String.valueOf(a);
}
return columnsName;
}
}