您的位置:首页 > 数据库 > MySQL

JDBC连接mysql代码

2006-11-16 09:46 381 查看
   <%
      try
   {
     String new_last_name="";
     String new_first_name="";
     Connection con=null;
     Statement stmt=null;
     Statement stmt1=null;
     ResultSet rs=null;
     request.setCharacterEncoding("GB2312");
     String employee_id=null;
     String last_name=request.getParameter("last_name");
     String first_name=request.getParameter("first_name");
     String birth="1982/7/28";
     String sex="F";
     String email="aaa@asdf.com";
    %>
   从mysql.html接收到的信息如下:<br>
   姓:<%=last_name %>
   名:<%=first_name %><br><br>
  
   <%
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     con=DriverManager.getConnection("jdbc:mysql://192.168.0.73:3306/chancedb?user=root&password=1&useUnicode=true&characterEncoding=GB2312");
     stmt=con.createStatement();
     String upd="INSERT INTO Sample_test(employee_id, last_name, first_name, birth, sex, email) VALUES ("+employee_id+",'"+last_name+"','"+first_name+"','"+birth+"','"+sex+"','"+email+"')";
     stmt.executeUpdate(upd);
     stmt1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     String query="SELECT * FROM Sample_test";
     rs=stmt1.executeQuery(query);
     rs.last();
     new_last_name=rs.getString("last_name");
     new_first_name=rs.getString("first_name");
     stmt.close();
     stmt1.close();
     con.close();
   }
   catch(SQLException sqle)
   {
    out.println("sqle=" +sqle);
   }
   finally
   {
    try{
     if(con !=null)
     {
      con.close();
     
     }
    
    }
    catch(SQLException sqle)
    {
     out.println("sqle="+sqle);
    }
   }
    
   %> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息