数据库中的html代码怎么替换显示

html-css07

数据库中的html代码怎么替换显示,第1张

数据库中的html代码怎么替换显示

一、链接数据库,通过sql语句获得你问题中要显示的内容,存到一个字符串变量Htmlstring里面。

二、在php页面中使用response.write("Htmlstring")

HTML是无法读取数据库的,HTML是页面前端脚本语言,要想从HTML网页中获取SQL数据库里的数据,需要借助JSP或ASP或PHP或RUBY等语言来实现。 简单的关系可以这样理解: 数据库<--->JSP或ASP或PHP或RUBY等语言<--->HTML

如:在JSP页面中显示完整代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

<%@ page

language="java"

contentType="text/htmlcharset=UTF-8"

pageEncoding="UTF-8"

%>

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

<center>

<H1><font color="blue" size="12">管理中心</font></H1>

<HR />

<table width="80%" border="1">

<tr>

<th>ID</th>

<th>书名</th>

<th>作者</th>

<th>价格</th>

<th>删除</th>

</tr>

<%

// 数据库的名字

String dbName = "zap"

// 登录数据库的用户名

String username = "sa"

// 登录数据库的密码

String password = "123"

// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1

String host = "127.0.0.1"

// 数据库的端口,一般不会修改,默认为1433

int port = 1433

String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username

+ "password=" + password

//

//声明需要使用的资源

// 数据库连接,记得用完了一定要关闭

Connection con = null

// Statement 记得用完了一定要关闭

Statement stmt = null

// 结果集,记得用完了一定要关闭

ResultSet rs = null

try {

// 注册驱动

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

// 获得一个数据库连接

con = DriverManager.getConnection(connectionUrl)

String SQL = "SELECT * from note"

// 创建查询

stmt = con.createStatement()

// 执行查询,拿到结果集

rs = stmt.executeQuery(SQL)

while (rs.next()) {

%>

<tr>

<td>

<%=rs.getInt(1)%>

</td>

<td>

<a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>

</td>

<td>

<%=rs.getString(3)%>

</td>

<td>

<%=rs.getString(4)%>

</td>

<td>

<a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">删除</a>

</td>

</tr>

<%

}

} catch (Exception e) {

// 捕获并显示异常

e.printStackTrace()

} finally {

// 关闭我们使用过的资源

if (rs != null)

try {

rs.close()

} catch (Exception e) {}

if (stmt != null)

try {

stmt.close()

} catch (Exception e) {}

if (con != null)

try {

con.close()

} catch (Exception e) {}

}

%>

</table>

<a href="insert.jsp">添加新纪录</a>

</center>