我已经构建了一个jsp页面,我从用户那里获取了输入并从该输入创建了一个表,现在我必须在另一个页面上显示该表,并且我还必须通过Java同时在mysql中创建该表。 / p>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dynamic table</title>
<script src ="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<SCRIPT language="javascript">
var counter=2;
function addRow(tableID){
var dynamicVal = 'row'+counter;
$('#dataTable').append('\
<TR>\
<TD><INPUT type="checkbox" name="'+dynamicVal+'_chk" id="'+dynamicVal+'_chk"/></TD>\
<TD><INPUT type="text" name="'+dynamicVal+'_col" id="'+dynamicVal+'_col" required/></TD>\
<TD>\
<SELECT name="'+dynamicVal+'_datatype" id="'+dynamicVal+'_datatype">\
<OPTION value="int">INT(40)</OPTION>\
<OPTION value="var">VARCHAR(40)</OPTION>\
<OPTION value="char">CHAR(40)</OPTION>\
</SELECT>\
</TD>\
</TR>');
counter++;
}
function deleteRow(tableID){
/*$('#dataTable').find('[INPUT type="checkbox"]').each(function(){\
if($this).prop("checked") == true({
($this).closest('tr').remove();
}
});*/
$('#dataTable').find('tr').each(function(){
if($(this).find('input[type="checkbox"]').prop('checked') == true){
$(this).remove();
}
});
}
function createJSON() {
var obj ={};
obj.table_name=$("input[id=table_name]").val();
var rowcolms = {};
var rowcollection = [];
$('#dataTable').find('tr').each(function(){
var row = {};
row.colName=$(this).find('input[type="text"]').val();
row.dataType = $(this).find('select').val();
rowcollection.push(row);
});
obj.tablecolms=rowcollection;
/*var col_name = $(this).val("text");
var datatype = $(this).type("select");
item = {}
item ["text"] = "col_name" ;
item ["select"] = "datatype";
jsonObj.push(item);
});
$*/
console.log(obj);
}
function ajaxcall(){
$.get.json({
url: Createdatabase,
type:"post",
data: jsonObj,
success: function(data){
console.log(data);
}
});
}
</SCRIPT>
</head>
<style>
body {
background-color : #D3D3D3;
margin: 0;
padding: 0;
}
h4 {
color : #000000;
text-align : center;
font-family: "SIMPSON";
}
form {
width: 300px;
margin: 0 auto;
}
</style>
<body>
<form>
<h4>Create Your Table</h4>
<form action="servlet/createdatabase" method="post">
Table Name: <input type="text" name="table_name" id="table_name"required>
<input type="submit" value="Submit"><br><br>
<h4>Design Your Table</h4>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" name="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="row1_chk" id="row1_chk"/></TD>
<TD><INPUT type="text" name="row1_col" id="row1_col" required/></TD>
<TD>
<SELECT name="row1_datatype" id="row1_datatype" type="select">
<OPTION value="int">INT(40)</OPTION>
<OPTION value="var">VARCHAR(40)</OPTION>
<OPTION value="char">CHAR(40)</OPTION>.
</SELECT>
</TD>
</TR>
</TABLE><br>
<input type="button" value="Create Table" onclick="ajaxcall'">
</form>
</body>
</html>
所以请任何人都可以告诉我如何在另一个页面上创建这个表,还能以表格的形式创建这个表并在mysql中创建这个表?
答案 0 :(得分:0)
JSP和MySQL之间的连接:下面的代码解释了首先创建连接。
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
要在第二页上显示数据,需要再次连接到DB,然后检索数据,有关详细信息,请查看此链接 mysql and jsp
答案 1 :(得分:0)
这是用于从JSP创建表,也是用mysql建立连接
<%@ page import="java.sql.*" %>
<HTML>
<HEAD>
<TITLE>Creating a Table</TITLE>
</HEAD>
<BODY>
<H1>Creating a Table</H1>
<%
Connection connection = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connection = DriverManager.getConnection("jdbc:odbc:data", "userName", "password");
Statement statement = connection.createStatement();
String command = "CREATE TABLE Employees (ID INTEGER, Name CHAR(50));";
statement.executeUpdate(command);
} catch (Exception e) {
out.println("An error occurred.");
}
%>
The Employees table was created.
</BODY>
</HTML>