
use
数据库名mysql>
source
d:/mysql.sql
2、建立数据库:mysql>
create
database
库名
3、建立数据表:mysql>
use
库名mysql>
create
table
表名
(字段名
varchar(20),
字段名
char(1))
4、删除数据库:mysql>
drop
database
库名
5、删除数据表:mysql>
drop
table
表名;
6、将表中记录清空:mysql>
delete
from
表名
7、往表中插入记录:mysql>
insert
into
表名
values
("hyq","m")
8、更新表中数据:mysql->
update
表名
set
字段名1='a',字段名2='b'
where
字段名3='c'
9、用文本方式将数据装入数据表中:mysql>
load
data
local
infile
"d:/mysql.txt"
into
table
表名
获取表单中的信息,然后插入到Mysql中<%@ page language="java" contentType="text/html charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
int id = Integer.parseInt(request.getParameter("id"))
int rootid = Integer.parseInt(request.getParameter("rootid"))
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=gbk">
<title>Replay</title>
</head>
<body>
<form method="post" action="ReplayOK.jsp">
<input type="hidden" name="id" value="<%=id %>">
<input type="hidden" name="rootid" value="<%=rootid %>">
<table align="center">
<tr>
<td>
<input type="text" name="title" size="80">
</td>
</tr>
<tr>
<td>
<textarea cols="80" rows="20" name="cont"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------
下面接收上面表单中传过来的信息,并插入到mysql中
<%@ page language="java" contentType="text/html charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("GBK")
int id = Integer.parseInt(request.getParameter("id"))
int rootid = Integer.parseInt(request.getParameter("rootid"))
String title = request.getParameter("title")
String cont = request.getParameter("cont").replaceAll("\n","<br/>")
Connection conn = null
Statement st = null
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=690115399")
st = conn.createStatement()
conn.setAutoCommit(false)
String sql = "insert into article values(null,?,?,?,?,now(),0)"
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setInt(1,id)
pstmt.setInt(2,rootid)
pstmt.setString(3,title)
pstmt.setString(4,cont)
pstmt.executeUpdate()
st.executeUpdate("update article set isleaf = 1 where id = " + id)
conn.commit()
conn.setAutoCommit(true)
st.close()
pstmt.close()
conn.close()
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=gbk">
<title>Insert title here</title>
</head>
<body>
<%response.sendRedirect("ShowArticleTree.jsp") %>
</body>
</html>
当然最好的方法还是应该用jsp + JavaBean方式。
servlet中一般都是直接调用逻辑层提供的方法(需要连接数据库)
逻辑层都是一个一个的java类,在java类中实现数据库连接
传统的方法,Class.forName()-DriverManager.getConnection()-conn(实例).createStatement或者prepareStatement-stmt(实例).execute()
数据库连接池:驱动放入web容器的lib,配置context.xml-利用JNDI技术获取数据源对象-该对象调用getConnection(),后面的 *** 作一样
Context ct=new InitialContext()DataSource ds=(DataSource)cx.lookUp("java:comp/env/jdbc/news(前缀+下面配置的Name值)") <Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="1234"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/newsmanagersystem?useUnicode=true&ampCharacterEncoding=UTF-8"
/>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)