用的最简单的jdbc连接数据库,拼sql语句插入数据库,得到前台传过来的用户的信息username 和userpass 然后存入数据库,这应该怎么写?public boolean addUser(User user) {
boolean flag = false;
try{
String sql = "insert into t_user values (null, '?', '?','normaluser')";
//System.out.println("here!!!!!!!!!!");//debug
this.pstmt = this.conn.prepareStatement(sql);
System.out.println(user.getUsername());//debug
this.pstmt.setString(1, user.getUsername());
this.pstmt.setString(2, user.getPassword());
this.pstmt.executeUpdate();
flag = true;
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(this.pstmt!=null){
this.pstmt.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return flag;
}我的插入数据库那一段代码是这样写的,dbconnection 我写成了 一个dbconnection的包 上面已经打开了连接 现在就是往数据库里写数据有问题 不知道sql是不是这样拼的?我的表里有四个字段 id auto_increment ,username, userpass, privilege(权限) 用的是mysql数据库。
boolean flag = false;
try{
String sql = "insert into t_user values (null, '?', '?','normaluser')";
//System.out.println("here!!!!!!!!!!");//debug
this.pstmt = this.conn.prepareStatement(sql);
System.out.println(user.getUsername());//debug
this.pstmt.setString(1, user.getUsername());
this.pstmt.setString(2, user.getPassword());
this.pstmt.executeUpdate();
flag = true;
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(this.pstmt!=null){
this.pstmt.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return flag;
}我的插入数据库那一段代码是这样写的,dbconnection 我写成了 一个dbconnection的包 上面已经打开了连接 现在就是往数据库里写数据有问题 不知道sql是不是这样拼的?我的表里有四个字段 id auto_increment ,username, userpass, privilege(权限) 用的是mysql数据库。
解决方案 »
- MyEclipse 的部署和启动tomcat的按钮不见了,怎么恢复
- JSP页面用电脑访问能自动刷新,用手机访问却不能自动刷新,为何??
- byte data[] = new byte[length];这个length最大长度为多少
- SSH+AJAX的小项目,同时在线人数400,要多大的硬件配置?
- axis如何生成服务端代码,具体步骤
- 问几个java面试的问题
- 给分:江湖告急。。求SQL Server 2000 Driver for JDBC Service Pack 4 驱动程序
- 哪里有struts api文档下呀,怎么我找的都是只能在线浏览呢??
- 想学JAVA
- java socket实现两个客户端之间的聊天
- csdn下载页面的资源怎么不见下载按钮或链接啊?
- jquery中使用struts标签的问题
如果你上面写的那些元素是所有的话,应该也是可以的。
但是我记得默认Id应该是主键,主键是不能为空的。你可以用hilo生成策略来解决。
如果往数据库里面写数据出问题的话,你把报的错发来上,也方便看啊。