欢迎加入站点基地设计开发交流群:30720409
详细内容
jsp连接mysql实例
发布日期:2010-03-04     点击:2672     字体:[ ]

  Class.forName("com.mysql.jdbc.Driver");    
  String   dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";    
   
  Connection   conn=DriverManager.getConnection(dsn);    
   
  Statement   cmd=conn.createStatement();    
   
  //新增数据
  String sql = "select max(id) as num from employee";
  ResultSet numRs = cmd.executeQuery(sql);
  numRs.next();

  int id = numRs.getInt(1) + 1;
 
  sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
  cmd.executeUpdate(sql);
 
  //查询数据
  ResultSet   rs=cmd.executeQuery("select *   from   employee");    
  
  while(rs.next())
  {
   out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
   out.println("<br/>");

  Class.forName("com.mysql.jdbc.Driver");    
  String   dsn="jdbc:mysql://localhost:3306/test_db?user=root&password=sa";    
   
  Connection   conn=DriverManager.getConnection(dsn);    
   
  Statement   cmd=conn.createStatement();    
   
  //新增数据
  String sql = "select max(id) as num from employee";
  ResultSet numRs = cmd.executeQuery(sql);
  numRs.next();

  int id = numRs.getInt(1) + 1;
 
  sql = "insert into employee(id,last_name,first_name,birth,sex,email) values(" + id + ",'cc,'dd','1982-11-16','M','abc@cc.com')";
  cmd.executeUpdate(sql);
 
  //查询数据
  ResultSet   rs=cmd.executeQuery("select *   from   employee");    
  
  while(rs.next())
  {
   out.println(rs.getString(1) + "--" + rs.getString(2) + "--" + rs.getString(3)+ "--" + rs.getString(4)+ "--" + rs.getString(5)+ "--" + rs.getString(6));
   out.println("<br/>");
  } 
  cmd.close();    
  rs.close();
  conn.close();  

  }

需下载连接数据驱动mysql-connector-java-5.1.0-bin.jar,这个文件可到站点基地下载,下载后将mysql-connector-java-5.1.0-bin.jar放置tomcat\webapps\ROOT\WEB-INF\lib目录下即可

下载附件:mysql-connector-java-5.1.0-bin.jar

用户评论
昵称 
内容  *
验证码   
   
相关文章 更多...  
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号-1 可人软件设计