随笔 - 64  文章 - 9  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(6)

我参与的团队

随笔分类(88)

随笔档案(92)

文章分类(142)

文章档案(182)

天基成员

学习园

我的海角

搜索

  •  

积分与排名

  • 积分 - 180563
  • 排名 - 318

最新评论

   1.  mysql
create database oa;
use oa;
create table employee(
id int primary key auto_increment,
name varchar(20),
sex varchar(2),
age int,
address varchar(50)
);
insert into employee values(null,'张冰','女',23,'河南南洋');
insert into employee values(null,'王冰','女',23,'北京');
insert into employee values(null,'张风','男',23,'河南南');
insert into employee values(null,'李洋','女',33,'河南南');

2.insert.html
<html>
<body>
<center>
<form action="doinsert.jsp" method="post">
username:<input type="text" name="username"><br>
sex:<input type="text" name="sex"><br>
age:<input type="text" name="age"><br>
address:<input type="text" name="address"><br>
<input type="submit" value=" submit ">
</form>
</body>
</html>

3.doinsert.jsp

<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<font size="20">
<%
  Connection con = null;
  PreparedStatement stmt = null;//使用预编语句声明

  String sql = "";
String name=request.getParameter("username");
name = new String(name.getBytes("iso-8859-1"),"gbk");
String sex=request.getParameter("sex");
sex = new String(sex.getBytes("iso-8859-1"),"gbk");
String age=request.getParameter("age");
String address=request.getParameter("address");
address = new String(address.getBytes("iso-8859-1"),"gbk");

sql = "insert into employee(name,sex,age,address) values(?,?,?,?)";//使用通配符?赋值占位

 try{
    Class.forName("com.mysql.jdbc.Driver");//加载驱动
   con= DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","yu");//获得连接
  
   stmt=con.prepareStatement(sql);//由于prepareStatement不用解释,安全,快速
    stmt.setString(1,name);//与通配符?一一对应,并给通配符赋值
    stmt.setString(2,sex);
    stmt.setInt(3,Integer.parseInt(age));//将字符串转化为int类型
    stmt.setString(4,address);
    stmt.executeUpdate();
   out.print("插入成功");

    }catch(Exception e){e.printStackTrace();}
%>
<br>
<a href="test.jsp">查看</a>
</font>
</body>
</html>


4.test.jsp

<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<font size="20">
<%
  Connection con = null;
  //Statement stmt = null;
  PreparedStatement stmt = null;
  ResultSet rs = null;
  String sql = "";
sql = "select * from employee";
 try{
    Class.forName("com.mysql.jdbc.Driver");
   con= DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","yu");
  //stmt = con.createStatement();
   stmt=con.prepareStatement(sql);//直接对sql语句进行处理,不必解释
  //rs =stmt.executeQuery(sql);
  rs =stmt.executeQuery();
while(rs.next())
 {
   out.print(rs.getString("name")+" "+rs.getString("sex")+" "+rs.getString("age")+" "+rs.getString("address")+"<br>");
 }

    }catch(Exception e){e.printStackTrace();}
%>
</font>
</body>
</html>


posted on 2009-04-21 11:21 鹏凌 阅读(531) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: