如何用SQL语言删除一个表

如何用SQL语言删除一个表,第1张

DROP命令。

1、drop table xx --xx是数据表的名字

作用:删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。

2、 drop table test,就是把整个表 移除.里面的数据都消失

比如下面有一个[FusionChartsDB]数据库中的test表。

3、执行 drop table FC_Products后,FC_Products删除的一干二净。

一般是跟jsp配合使用的,这里有个完整的例子供参考:

<%@page import="javax.swing.JOptionPane"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*" %>

<%@page import="java.util.*" %>

<%@page import="com.google.gson.*" %>

<%

String hostdb = "localhost:3306"// MySQl 主机名

String userdb = "root"// MySQL 用户名

String passdb = ""// MySQL 密码

String namedb = "fusioncharts_jspsample"// MySQL 数据库名

// 建立数据库连接

DriverManager.registerDriver(new com.mysql.jdbc.Driver())

Connection con = DriverManager.getConnection("jdbc:mysql://" + hostdb + "/" + namedb , userdb , passdb)

st = con.createStatement()

%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>Creating Charts with Data from a Database - fusioncharts.com</title>

<!-- Step 1: Include the `fusioncharts.js` file. This file is needed to

render the chart. Ensure that the path to this JS file is correct.

Otherwise, it may lead to JavaScript errors.

-->

<script src="fusioncharts.js"></script>

</head>

<body>

<div id="chart"></div>

<!-- Step 2: Include the `FusionCharts.java` file as a package in your

project.

-->

<%@page import="fusioncharts.FusionCharts" %>

<!-- Step 3:Include the package in the file where you want to show

FusionCharts as follows.

Step 4: Create a chart object using the FusionCharts JAVA class

constructor. Syntax for the constructor:

`FusionCharts("type of chart", "unique chart id", "width of chart",

"height of chart", "div id to render the chart",

"data format", "data source")`

-->

<%

/*

google-gson

Gson is a Java library that can be used to convert Java Objects into

their JSON representation. It can also be used to convert a JSON string to

an equivalent Java object. Gson can work with arbitrary Java objects including

pre-existing objects that you do not have source-code of.

link : https://github.com/google/gson

*/

Gson gson = new Gson()

// Form the SQL query that returns the top 10 most populous countries

// String sql="SELECT * FROM stackedbarchart"

rs = st.executeQuery("SELECT * FROM stackedbarchart")

// Execute the query.

// PreparedStatement pt=con.prepareStatement(sql)

// ResultSet result=pt.executeQuery()

System.out.println("Result of sql one"+rs)

// The 'chartobj' map object holds the chart attributes and data.

Map<String, String>chartobj = new HashMap<String, String>()//for getting key value pair

chartobj.put("caption", "Product-wise quarterly revenue in current year")

chartobj.put("subCaption", "Harrys SuperMart")

chartobj.put("captionFontSize", "14")

chartobj.put("subcaptionFontSize", "14")

chartobj.put("subcaptionFontBold", "0")

chartobj.put("paletteColors", "#0075c2,#1aaf5d")

chartobj.put("bgcolor", "#ffffff")

chartobj.put("showBorder", "0")

chartobj.put("showShadow", "0")

chartobj.put("showCanvasBorder", "0")

chartobj.put("valueFontColor","#ffffff")

chartobj.put("usePlotGradientColor", "0")

chartobj.put("legendBorderAlpha", "0")

chartobj.put("legendShadow", "0")

chartobj.put("showAxisLines", "0")

chartobj.put("showAlternateHGridColor", "0")

chartobj.put("divlineThickness", "1")

chartobj.put("divLineDashed", "1")

chartobj.put("divLineDashLen", "1")

chartobj.put("divLineGapLen", "1")

chartobj.put("xAxisName", "Quarter")

chartobj.put("yAxisName","Revenue(In USD)")

chartobj.put("showValues", "1")

chartobj.put("showHoverEffect","1")

//prepare categories

ArrayList categories = new ArrayList()

ArrayList dataset = new ArrayList()

int id1,id2,id3

String a =""

while (rs.next())

{

id1=rs.getInt(3)

id2=rs.getInt(4)

//id3=rs.getInt(2)

a=rs.getString(2)

System.out.println("Result of sql two1"+id1)

System.out.println("Result of sql two2"+id2)

System.out.println("Result of sql two3"+a)

//System.out.println("Result of sql two3"+id3)

}

categories.add(buildCategories("label",rs,gson))

System.out.println("Result of sql buildCategories"+buildCategories("label",rs,gson))

dataset.add(buildDataset("RamcoCount","ramco_imp", rs, gson))

dataset.add(buildDataset("PartnerCount", "partner_imp", rs, gson))

System.out.println("Result of sql buildDataset"+buildDataset("PartnerCount", "partner_imp", rs, gson))

// }System.out.println("Result of sql two"+categories)

//prepare dataset

System.out.println("Result of sql three"+dataset)

//close the connection.

rs.close()

//create 'dataMap' map object to make a complete FusionCharts datasource.

Map<String, String>dataMap = new LinkedHashMap<String, String>()

/*

gson.toJson() the data to retrieve the string containing the

JSON representation of the data in the array.

*/

dataMap.put("chart", gson.toJson(chartobj))

dataMap.put("categories", gson.toJson(categories))

dataMap.put("dataset", gson.toJson(dataset))

FusionCharts mslineChart= new FusionCharts(

"stackedbar2d",// chartType

"chart1",// chartId

"600","400",// chartWidth, chartHeight

"chart",// chartContainer

"json",// dataFormat

gson.toJson(dataMap) //dataSource

)

System.out.println(dataMap)

%>

<%!

/**

* @description - Build the Json for the categories

* @param {String} data_item - Name of the column from table

* @param {ResultSet} rs - The object of ResultSet maintains a

* cursor pointing to a particular row of data.

* @param {Gson} gson - Gson is a Java library that can be used

* to convert Java Objects into their JSON representation.

* @return {Map Object}

*/

public Map buildCategories(String data_item, ResultSet rs,Gson gson) {

//creation of the inner category

Map<String, String>categoryinner = new HashMap<String, String>()

ArrayList category = new ArrayList()

int counter = -1

try {

//to restore the position of the result set.

rs.beforeFirst()

while(rs.next()) {

//for creating the key value for the category label from database.

Map<String, String>lv = new HashMap<String, String>()

lv.put("label", rs.getString(data_item))

category.add(lv)

counter ++

}

categoryinner.put("category", gson.toJson(category))

System.out.println(categoryinner)

}catch(Exception ex) {/* if any error occurs */}

return categoryinner

}

/**

* @description - Build the Json for datasets

* @param {String} seriesname - Lets you specify the series

* name for a particular dataset.

* @param {String} seriescolumnname - Name of the column from table

* @param {ResultSet} - The object of ResultSet maintains a

* cursor pointing to a particular row of data.

* @param {Gson} gson - Gson is a Java library that can be used

* to convert Java Objects into their JSON representation.

* @return {Map Object}

- */

public Map buildDataset(String seriesname, String seriescolumnname, ResultSet rs, Gson gson ) {

Map<String, String>datasetinner = new HashMap<String, String>()

datasetinner.put("seriesname", seriesname)

ArrayList makedata = new ArrayList()

try {

//is used to move the cursor to the first row in result set object.

rs.beforeFirst()

while(rs.next()) {

Map<String, String>preparedata = new HashMap<String, String>()

preparedata.put("value", rs.getString(seriescolumnname))

makedata.add(preparedata)

}

datasetinner.put("data", gson.toJson(makedata))

} catch(Exception err) {/* if any error occurs */}

return datasetinner

}

%>

<!-- Step 5: 渲染chart-->

<%= mslineChart.render() %>

</body>

</html>

第一步:用asp连接access查询数据表中的数据。

第二步:根据fusionchart 调用的js文件中的"data": [{..}]部分,按照固定格式输出;

此步骤是关键,就是按照fusionchart 要求输出即可,有两种方法,1是在当前HTML页面中直接通过asp的 response.write(rs("value"))输出数据库内容。2是输出内容以后保存js文档,供html调用。

第三步:正常调用fusionchart 输出图表;

举例子:

FusionCharts.ready(function () {

    var revenueChart = new FusionCharts({

        type: 'doughnut2d',

        renderAt: 'chart-container',

        width: '450',

        height: '450',

        dataFormat: 'json',

        dataSource: {

            "chart": {

                "caption": "Split of Revenue by Product Categories",

                "subCaption": "Last year",

                "numberPrefix": "$",

                "paletteColors": "#0075c2,#1aaf5d,#f2c500,#f45b00,#8e0000",

                "bgColor": "#ffffff",

                "showBorder": "0",

                "use3DLighting": "0",

                "showShadow": "0",

                "enableSmartLabels": "0",

                "startingAngle": "310",

                "showLabels": "0",

                "showPercentValues": "1",

                "showLegend": "1",

                "legendShadow": "0",

                "legendBorderAlpha": "0",

                "defaultCenterLabel": "Total revenue: $64.08K",

                "centerLabel": "Revenue from $label: $value",

                "centerLabelBold": "1",

                "showTooltip": "0",

                "decimals": "0",

                "captionFontSize": "14",

                "subcaptionFontSize": "14",

                "subcaptionFontBold": "0"

            },

            "data": [

                //原js文件中的格式要求部分

                //{

                //    "label": "Food",

                //    "value": "28504"

                //}, 

                //{

                //    "label": "Apparels",

                //    "value": "14633"

                //}, 

                //{

                //    "label": "Electronics",

                //    "value": "10507"

                //}, 

                //{

                //    "label": "Household",

                //    "value": "4910"

                //}

                //注意这里开始用asp输出

                

                <%

                for i=1 to rs.recordcount

if rs.eof then exit for

                

                if i<rs.recordcount then

                    response.write("{"label":" & rs("name") & ",")

                    response.write(""value":") & rs("value") & "},")

                else

                    response.write("{"label":" & rs("name") & ",")

                    response.write(""value":") & rs("value") & "}")

                end if

                

                rs.movenext:next

                %>

            ]

        }

    }).render()

})


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10012059.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存