插入数据库的方法public void up(String name)
{
String sql="insert into userScore(name) values ('"+name+"')";
this.name=name;
try {
st = this.getConn().createStatement() ;
st.executeUpdate(sql) ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
添加的页面
<body>
<p align="center">
<font size="50">添加用户</font>
<form action="updateUser.jsp" method="post">
姓名:<input type="text" name="username"><br>
<input type="submit" value="添加用户">
<input type="reset" value="取消"> </form>
updateUser.jsp
<% String n=(String)request.getParameter("username");
System.out.println(n+" " );
new DBbase().up(n);
response.sendRedirect("show.jsp") ;
%>
可是就是添加不到数据库
测试出来的n是输数字能出来 输文字就出来的是???问号 实在是不懂为什么
{
String sql="insert into userScore(name) values ('"+name+"')";
this.name=name;
try {
st = this.getConn().createStatement() ;
st.executeUpdate(sql) ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
添加的页面
<body>
<p align="center">
<font size="50">添加用户</font>
<form action="updateUser.jsp" method="post">
姓名:<input type="text" name="username"><br>
<input type="submit" value="添加用户">
<input type="reset" value="取消"> </form>
updateUser.jsp
<% String n=(String)request.getParameter("username");
System.out.println(n+" " );
new DBbase().up(n);
response.sendRedirect("show.jsp") ;
%>
可是就是添加不到数据库
测试出来的n是输数字能出来 输文字就出来的是???问号 实在是不懂为什么
目测几个错误,SQL拼错了,汉字问号是中文乱码,你把JSP的ISO-8809-1全换成UTF-8或GBK
String sql="insert into userScore(name) values (?);
this.getConn().prepareStatement(sql);
ps.setString(1,name)
浏览器-》查看-》编码格式,换成GBK或者UTF-8看看。
String sql="insert into userScore(name) values ('"+name+"')";
这个应该没有大毛病,但是个空格吧
String sql="insert into userScore (name) values ('"+name+"')";
另外更新完了是否需要提交我忘了。
但是加上提交绝对不会报错。