package TestSql;import java.sql.*;
public class SqlAdd {
public static void main(String args[]){
 String userName="root";//数据库用户名             String userPasswd="520093";//密码             String dbName="nmsdb";//数据库名             String tableName="comment_info";//表名             String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;//联结字符串
             try{
                         Class.forName("com.mysql.jdbc.Driver").newInstance();
                 
                 Connection connection=DriverManager.getConnection(url);
                 Connection c1 = DriverManager.getConnection(url);
                 Statement statement = connection.createStatement();
                // Statement s1 = c1.createStatement();
                 //ResultSet r1=s1.executeUpdate( "INSERT INTO "+tableName+" VALUES (4,1,成功了,0,0,,darcyg)");
                 String add = "INSERT INTO "+tableName+" VALUES (4,1,成功了,0,0,,darcyg)";
                 //statement.executeUpdate(add);
                 String sql1="SELECT * FROM "+tableName;
                 ResultSet rs1 = statement.executeQuery(sql1); 
                 
             while(rs1.next()){
              System.out.println(rs1.getString("content"));
             }
             }catch(Exception e){
                    e.printStackTrace();
                  }
                   

}
}
现在的问题是注释掉statement.executeUpdate(add);这行则可以在控制台看见内容..不注释的话eclipse也不报错,但控制台什么也看不见..忘高手指教一下..

解决方案 »

  1.   

     String add = "INSERT INTO "+tableName+" VALUES (4,1,成功了,0,0,,darcyg)";
    字符串类型值,至少要用单引号括起来, 如'成功了', 'darcyg' 等,中间居然还多了逗号 ??
      

  2.   

    String add = "INSERT INTO "+tableName+" VALUES (4,1,成功了,0,0,,darcyg)";类似问题,建议断点调试,然后看一下你的字符串实际内容是什么,再直接到数据库中去执行一下,数据库会明确告诉你哪错了。