想要向mysql里存时间,写了一demo但是总有问题使用的java mysql类型都是timestamp程序分两个线程一个连接数据库 建表存时间
另一个删掉表 断开数据库
中间sleep()几秒
import java.nio.channels.ClosedByInterruptException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;import java.text.SimpleDateFormat;
import java.util.Date; public class split extends Thread {
static Connection conn ;
   
    public static void main(String[] args) {
     String driver = "com.mysql.jdbc.Driver";      // URL指向要访问的数据库名scutcs      String url = "jdbc:mysql://127.0.0.1:3306/HELLO";      // MySQL配置时的用户名      String user = "root";      // Java连接MySQL配置时的密码      String password = "root";      //Connection conn=null;
     Statement statement=null;
    
try { // 加载驱动程序 Class.forName(driver); // 连续数据库  conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()){
System.out.println("Succeeded connecting to the Database!");

}
statement = conn.createStatement();


thread1 t1=new thread1(statement,conn);
     thread2 t2=new thread2(statement,conn);
     t1.start();
     Thread.sleep(2000);
     t2.start();

System.out.println("数据库被关闭");
}catch(ClassNotFoundException e) {   
System.out.println("Sorry,can`t find the Driver!");   
e.printStackTrace();   
} catch(SQLException e) {   
e.printStackTrace();   
} catch(Exception e) {   
e.printStackTrace();   

    


    }
    
    
}

class thread1 extends Thread
{
public Statement stat=null;
public Connection conn=null;
thread1(Statement stat,Connection conn)
{
this.stat=stat;
this.conn= conn;
}
public void run()
{
String str="th";
String strs=str+"stable";
//SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     //Date date = new Date(); 
     //System.out.println(bartDateFormat.format(date));
      Date date = new Date();//获得系统时间.
          String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//将时间格式转换成符合Timestamp要求的格式.
          Timestamp newdate = Timestamp.valueOf(nowTime);//把时间转换
         
          //yyyy-MM-dd
try {
stat.executeUpdate("CREATE TABLE "+strs+"(datet TIMESTAMP)");
stat.executeUpdate("INSERT INTO "+strs+" (datet) VALUES ("+newdate+")");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("建表");
}
}
class thread2 extends Thread
{
public Statement stat=null;
public Connection conn=null;
thread2(Statement stat,Connection conn)
{
this.stat=stat;
this.conn=conn;

}
public void run()
{
String str="th";
String strs=str+"stable";
try {
stat.executeUpdate("DROP TABLE "+strs);
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删表");
}
} 报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '11:08:04.0)' at line 1线程没有问题,就是存时间这一块
String str="th";
String strs=str+"stable";
//SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     //Date date = new Date(); 
     //System.out.println(bartDateFormat.format(date));
      Date date = new Date();//获得系统时间.
          String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//将时间格式转换成符合Timestamp要求的格式.
          Timestamp newdate = Timestamp.valueOf(nowTime);//把时间转换
         
          //yyyy-MM-dd
try {
stat.executeUpdate("CREATE TABLE "+strs+"(datet TIMESTAMP)");
stat.executeUpdate("INSERT INTO "+strs+" (datet) VALUES ("+newdate+")");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("建表");第一次用mysql,求大家指导啊,菜鸟很菜

解决方案 »

  1.   

    stat.executeUpdate("CREATE TABLE "+strs+"(datet TIMESTAMP)");
    stat.executeUpdate("INSERT INTO "+strs+" (datet) VALUES ("+newdate+")");这两句先把sql语句存在String里打印出来看看。
    我猜sql语句有错误。
    String sql = "CREATE TABLE "+strs+"(datet TIMESTAMP)";
    System.out.println(sql);
    stat.executeUpdate(sql);sql = "INSERT INTO "+strs+" (datet) VALUES ("+newdate+")";
    System.out.println(sql);
    stat.executeUpdate("INSERT INTO "+strs+" (datet) VALUES ("+newdate+")");
      

  2.   

    打印出来是这样的
    CREATE TABLE thstable(datet TIMESTAMP)
    INSERT INTO thstable (datet) VALUES (2012-08-21 11:33:45.0)
      

  3.   

    嗯……我要说的就是永远不要用Statement,用PreparedStatement代替吧。
      

  4.   

    stat.executeUpdate("INSERT INTO "+strs+" (datet) VALUES ('"+newdate+"')");
      

  5.   


    嗯,用了preparedstatement出现一个问题,我想设置 创建的表名PreparedStatement ps = (PreparedStatement) conn.prepareStatement("CREATE TABLE ?  (datet DATETIME)");
    ps.setString(1, "myt");
    ps.executeUpdate();
    出现了这样的错误
     You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''myt'  (datet DATETIME)' at line 1是我的搁置不对吗
      

  6.   

    嗯,应该是这样的,现在我用了preparedstatement出现了新问题,在楼上,希望你能帮我看看
      

  7.   

    我当时做毕业设计的时候也遇到过这个问题。后来各种转换格式,因为用到了时间控件,太麻烦了,后来果断放弃了,在数据库中用vchar字符串形式的就很好地解决了。
      

  8.   

    1 直接insert方法,前后必须加上单引号
    2 prepared的方法,是setDate(1,myt); 别加单引号
      

  9.   

    因为myt是一个字符串,不加引号的话,会报错。