java与数据库
以下代码是java代码,我想在D:\my_data.mdb中添加一条记录,程序能运行,但是不能达到预期结果(不能正确的添加记录)?也可以加QQ为我解答!QQ:258353899先谢谢了!
import java.sql.*;
import java.util.*;
public class MessageBean
{
private Connection con;
public MessageBean()
{
String CLASSFORNAME= "sun.jdbc.odbc.JdbcOdbcDriver ";
String SERVANDDB= "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\my_data.mdb ";
try
{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//添加记录
void addMessage()
{ String s1= "关于XXX ";
String s2= "小宋 ";
String s3= "sfq1984sxs@126.com ";
String s4= "增加一些测试的内容! ";
try
{
PreparedStatement stm=con.prepareStatement( "insert into message values(?,?,?,?,?) ");
stm.setString(1,s1);
stm.setString(2,s2);
stm.setString(3,s3);
stm.setString(4,s4);
try{
stm.executeUpdate();
}
catch(Exception e)
{
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String arg[])
{
MessageBean pro=new MessageBean();
pro.addMessage();
System.out.println( "数据添加成功! ");
}
}
------解决方案--------------------LZ至少要把产生什么例外 发出来吧
------解决方案--------------------PreparedStatement stm=con.prepareStatement( "insert into message values(?,?,?,?,?) ");
stm.setString(1,s1);
stm.setString(2,s2);
stm.setString(3,s3);
stm.setString(4,s4);
五个参数,怎么设了四个值,第五个呢?
------解决方案--------------------楼上的够细心呀
PreparedStatement stm=con.prepareStatement( "insert into message values(?,?,?,?) ");
stm.setString(1,s1);
stm.setString(2,s2);
stm.setString(3,s3);
stm.setString(4,s4)