jsp向数据库中插入数据的sql命令
发布网友
发布时间:2022-04-30 09:55
我来回答
共4个回答
热心网友
时间:2022-04-10 14:46
以mysql为例子给一个代码示例:如下
public class MemberDAO {
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER = "root";
public static final String DBPASS = "1234";
private PreparedStatement ps = null;
private Connection conn = null;
public MemberDAO() {
//DatabaseConnection dbc = new DatabaseConnection();
//this.conn = dbc.getConnection(conn);
}
//---------------------------------------插入信息----------------------------------
public boolean doInsert(Member member) throws Exception {
boolean flag = false;
String sql = "insert into member values(?,?,?,?,?,?,?,?,?)";
Connection conn = null;
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
ps = conn.prepareStatement(sql);
ps.setString(1, member.getUserid());
ps.setString(2, member.getPassword());
ps.setString(3, member.getName());
ps.setString(4, member.getSex());
ps.setString(5, member.getPhone());
ps.setString(6, member.getEmail());
ps.setString(7, member.getInterest1());
ps.setString(8, member.getInterest2());
ps.setString(9, member.getNote());
if (ps.executeUpdate() > 0) {
flag = true;
}
conn.close();
ps.close();
return flag;
}
}
热心网友
时间:2022-04-10 16:04
sql="insert into test (ID,title,describe,autor,matter) values ("+ID+",'"+title+"','"+describe+"','"+autor+"','"+matter+"')";追问我就是这样写的啊 不行
追答sql="insert into test (`ID`,`title`,`describe`,`autor`,`matter`) values ("+ID+",'"+title+"','"+describe+"','"+autor+"','"+matter+"')";
热心网友
时间:2022-04-10 17:39
值不对吧
热心网友
时间:2022-04-10 19:30
不是数据库的问题,就是语句的问题。建表语句有没有