MyEclipse------PreparedStatement使用方法

testPreparedStatement.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testPrepareStatement.jsp' starting page</title>
</head>

<body>
    <%
        String url="jdbc:mysql://localhost:3306/student?useSSL=true";
        String useName="root";
        String password="2277092";
        
        Connection conn=null;
        Statement stmt=null;
        
        try{
            Class.forName("com.mysql.jdbc.Driver");
            //out.print("加载驱动类成功");
        }
        catch(ClassNotFoundException e){
            out.print(e);
        }
        
        try{
        conn=DriverManager.getConnection(url,useName,password);
        
        PreparedStatement pstmtInsert=conn.prepareStatement(
            "insert into classinfo(no,name,sex,major,phone,address,age,scoure) values(?,?,?,?,?,?,?,?);");

        pstmtInsert.setInt(1, 6);
        pstmtInsert.setString(2,"沐白");
        pstmtInsert.setString(3, "");
        pstmtInsert.setString(4,"网络工程");
        pstmtInsert.setString(5, "2277092");
        pstmtInsert.setString(6,"五邑大学");
        pstmtInsert.setInt(7, 23);
        pstmtInsert.setInt(8,90);
            
        pstmtInsert.executeUpdate();
        
        pstmtInsert.close();
        out.print("插入数据成功");
        }
        catch(SQLException e){
            out.print(e);
        }
        finally{
            try{
                if(conn!=null)
                    conn.close();
            }
            catch(Exception e){
                out.print("断开数据库连接出现异常");
            }
        }
     %>
</body>
</html>
原文地址:https://www.cnblogs.com/tianhengblogs/p/5324512.html