java代码070

虾米大王
• 阅读 327

code070.java

package pack02;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//SaveServlet��
@WebServlet(name = "code070",urlPatterns = "/page04/code070")
public class code070 extends HttpServlet
{
    private Connection conn = null;

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        //��ʼ��ʱ�������ݿ⽨������
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "mysql123";
        try
        {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(url,user,password);
            if (this.conn != null)
            {
                System.out.println("���ݿ����ӳɹ���");
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String description = request.getParameter("description");
        if (conn != null)
        {
            try
            {
                String sql = "insert into user(user_name,password,sex,description)"
                + "values(?,?,?,?)";
                 PreparedStatement param = conn.prepareStatement(sql);
                param.setString(1, userName);
                param.setString(2, pwd);
                param.setString(3, sex);
                param.setString(4, description);
                param.executeUpdate();

                PrintWriter writer1 = response.getWriter();
                writer1.print("<h1>");
                writer1.print(userName + "ע��ɹ�");
                writer1.print("</h1>");
                writer1.flush();
                writer1.close();
                conn.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else 
        {
            response.sendError(500,"���ݿ����Ӵ���");
        }
    }
}


/*
 *servlet3.0������
 *����ע����servlet3.0���ش����֮һ��
 *ͨ��ʹ��ע�;�������web.xml�ļ��ж�servlet���߹�����
 *���������ˡ�
 *����ע����@WebServlet��@WebFilter��@WebListener��@WebInitParam
 * 
 * @WebServlet���������£�
 * name string        �ȼ���<servlet-name>  
 * value string[]  �ȼ��� urlPatterns ����������ͬʱʹ��
 * urlPatterns string[]     �ȼ���<url-pattern>
 * loadOnStartup int     �ȼ���<load-on-startup>
 * initParams WebInitParam[]    �ȼ���<init-param>
 * asyncSupported boolean    �ȼ���<async-supported>
 * description sring    �ȼ���<description>
 * displayName string      �ȼ���<display-name>
 */
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
虾米大王 虾米大王
2年前
java代码087
code087.javapackagepack02;importjava.sql.CallableStatement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;import
虾米大王 虾米大王
2年前
java代码079
code079.javapackagepack02;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.s
虾米大王 虾米大王
2年前
java代码073
code073.javapackagepack02;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.UUID;importjavax.servlet.ServletException;importjavax.servlet.annotation.Multip
虾米大王 虾米大王
2年前
java代码084
code084.javapackagepack02;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.SQLException;importjava.util.Random;publicc
虾米大王 虾米大王
2年前
java代码090
code090.javapackagepack02;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;import
虾米大王 虾米大王
2年前
java代码083
code083.javapackagepack02;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjavax.servlet.ServletExcept
虾米大王 虾米大王
2年前
java代码082
code082.javapackagepack02;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjavax.servlet.ServletExcept
Wesley13 Wesley13
3年前
JDBC对MySQL的基本操作
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;impor
虾米大王
虾米大王
Lv1
男 · java工程师
梦里相思,故国王孙路。
文章
100
粉丝
1
获赞
100