往Access往数据库中插入数据如
stmt.executeUpdate("insert into table_1 values('userName','userPass')");总提示错误是什么原因啊?
提示的错误是查询值的数目和目标字段中的值不同。
再说一下userName和userPass分别是从图形用户界面上的文本框和密码框中获取的作为参数传到这里的。数据库中该字段的属性都是文本型。
stmt.executeUpdate("insert into table_1 values('userName','userPass')");总提示错误是什么原因啊?
提示的错误是查询值的数目和目标字段中的值不同。
再说一下userName和userPass分别是从图形用户界面上的文本框和密码框中获取的作为参数传到这里的。数据库中该字段的属性都是文本型。
PreparedStatement perstmt ;
perstmt = con.prepareStatement("insert into table_1 (userName,userPass) values (?,?)");
perstmt.setString(1,userName);
perstmt.setString(2,userPass);
perstmt.executeUpdate();
//这样也可以 如果不用预编译
stmt.executeUpdate("insert into table_1 values('" + userName + "','" + userPass + "')");
String b(String userName, String userPass){
String str="";
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){System.out.println(""+e);
}
try{conn = DriverManager.getConnection("jdbc:odbc:ljd","","");
stmt = conn.createStatement();
//stmt.executeUpdate("insert into table_1 values('userName','userPass')");
stmt.executeUpdate("insert into table_1 values('" + userName + "','" + userPass + "')");
rs=stmt.executeQuery("select * from table_1 where s_name=userName");
while(rs.next()){ String name=rs.getString(1);
String pw=rs.getString(2);
str="用户名:"+name+","+"密码:"+pw;
}
}
catch(SQLException e){System.out.println(e);}
finally{
try{
if(rs != null){rs.close();}
if(stmt != null){stmt.close();}
if(conn != null){conn.close();}
}
catch (Exception e){ e.printStackTrace(); }
}
return str;
}