Java连接mysql数据库并插入中文数据显示乱码??
EmpDao.javapackagecom.cdd;publicclassEmpDao{Connectionconn=null;publicConnectiongetCo...
EmpDao.java
package com.cdd;
public class EmpDao {
Connection conn = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功!!");
String url = "jdbc:mysql://localhost:3306/db_database03";
String user = "root";
String passWord = "111";
conn = DriverManager.getConnection(url, user, passWord);
System.out.println("已成功的与MySQL数据库建立连接!!");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public String insertEmp(Emp emp){
conn = getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into tb_emp values(null,?,?,?,?,?,?)"); //定义插入员工信息的SQL语句
statement.setString(1, emp.getName()); //设置预处理语句参数值
statement.setInt(2, emp.getAge()); //设置预处理语句参数值
statement.setString(3, emp.getSex());
statement.setString(4, emp.getDept());
statement.setString(5, emp.getPhone());
statement.setString(6, emp.getRemark());
statement.executeUpdate(); //执行插入语句
conn.close(); //关闭数据库连接
return "数据插入成功"; //返回执行结果
} catch (Exception e) {
e.printStackTrace();
return "数据插入失败";
}
}
}
InsertServlet.java
package com.cdd;
public class InsertServlet extends HttpServlet {
public InsertServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Emp emp = new Emp();
emp.setName(request.getParameter("nameTextfield"));
emp.setAge(Integer.parseInt(request.getParameter("ageTextfield")));
emp.setSex(request.getParameter("sexSelect"));
emp.setDept(request.getParameter("deptTextfield"));
emp.setPhone(request.getParameter("phoneTextfield"));
emp.setRemark(request.getParameter("remarkTextarea"));
EmpDao empDao = new EmpDao();
String message = empDao.insertEmp(emp);
request.setAttribute("message", message);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
public void init() throws ServletException {
}
}
Emp.java和index.jsp就不写了
id为11的那行是通过Java添加的,其它的是通过mysql数据库添加的。 展开
package com.cdd;
public class EmpDao {
Connection conn = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功!!");
String url = "jdbc:mysql://localhost:3306/db_database03";
String user = "root";
String passWord = "111";
conn = DriverManager.getConnection(url, user, passWord);
System.out.println("已成功的与MySQL数据库建立连接!!");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public String insertEmp(Emp emp){
conn = getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into tb_emp values(null,?,?,?,?,?,?)"); //定义插入员工信息的SQL语句
statement.setString(1, emp.getName()); //设置预处理语句参数值
statement.setInt(2, emp.getAge()); //设置预处理语句参数值
statement.setString(3, emp.getSex());
statement.setString(4, emp.getDept());
statement.setString(5, emp.getPhone());
statement.setString(6, emp.getRemark());
statement.executeUpdate(); //执行插入语句
conn.close(); //关闭数据库连接
return "数据插入成功"; //返回执行结果
} catch (Exception e) {
e.printStackTrace();
return "数据插入失败";
}
}
}
InsertServlet.java
package com.cdd;
public class InsertServlet extends HttpServlet {
public InsertServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Emp emp = new Emp();
emp.setName(request.getParameter("nameTextfield"));
emp.setAge(Integer.parseInt(request.getParameter("ageTextfield")));
emp.setSex(request.getParameter("sexSelect"));
emp.setDept(request.getParameter("deptTextfield"));
emp.setPhone(request.getParameter("phoneTextfield"));
emp.setRemark(request.getParameter("remarkTextarea"));
EmpDao empDao = new EmpDao();
String message = empDao.insertEmp(emp);
request.setAttribute("message", message);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
public void init() throws ServletException {
}
}
Emp.java和index.jsp就不写了
id为11的那行是通过Java添加的,其它的是通过mysql数据库添加的。 展开
展开全部
数据库连接字段改为
jdbc:mysql://localhost:3306/db_database03?useUnicode=true&characterEncoding=utf8
数据库创建的时候
create database db_database03 character set utf8
jdbc:mysql://localhost:3306/db_database03?useUnicode=true&characterEncoding=utf8
数据库创建的时候
create database db_database03 character set utf8
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将那张表给删了,重新建表,特别要注意数据库的编码,一般情况下用utf-8基本不会有乱码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询