从Java中插入mysql数据库jdbc失败

时间:2016-04-15 18:39:52

标签: java mysql jdbc

我无法从我的Java应用程序插入从我的服务器到我的MySQL数据库。从我做插入的循环开始,第一个工作没有任何问题,但除了插入之外什么也没做,要么应用程序失败,要么收到任何错误。

private Database _connect() {
    try {
    if (this._conn == null || this._conn.isValid(0)) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Properties connProps = new Properties();
            connProps.put("user", Config.Config.DB_USER);
            connProps.put("password", Config.Config.DB_PASS);
            this._conn = DriverManager.
                        getConnection("jdbc:" + Config.Config.DB_DBMS + "://" + Config.Config.DB_HOST + ":"
                                + Config.Config.DB_PORT + "/" + Config.Config.DB_NAME, Config.Config.DB_USER, Config.Config.DB_PASS);
            timer = System.currentTimeMillis();
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Could not connect to DB");
            e.printStackTrace();
        }
    } else {
        try {
            long tmp = System.currentTimeMillis() - timer;
            if (tmp > 1200000) { //3600000 one hour ; 1200000 twenty minutes
                System.out.println("Forcing reconnection ("+tmp+" milliseconds passed since last connection)");
                this.close();
                this._connect();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Forcing reconnection");
            this._conn = null;
            this._connect();
        }
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return this;
}

所以插入:

public class File {
/*db preventive*/
private Statement sment = null;
private PreparedStatement psment = null;
private ResultSet rset = null;


public File(){

}

public boolean insert(Schema file) {
    Connection conn = Database.connect().get();
    String sql = "";
    try {
        sql = "";
        psment = conn.prepareStatement("INSERT INTO files (evento_id, user_id, path, thumb, preview, width, height, md5, numero_corredor, created, modified) "
                                        + "VALUES (?,?,?,?,?,?,?,?,?,NOW(),NOW())");
        psment.setInt(1, file.getEventId());
        psment.setInt(2, file.getUserId());
        psment.setString(3, file.getPath());
        if (file.getPreview() == null)
            psment.setNull(4, java.sql.Types.VARCHAR);
        else
            psment.setString(4, file.getPreview());
        psment.setString(5, file.getThumb());

        psment.setInt(6, file.getWidth());
        psment.setInt(7, file.getHeight());
        psment.setString(8, file.getMd5());
        psment.setString(9, "sin_numero");
        psment.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
        //if there's error, better to have the sql insert at hand to do it manually
        FileWriter fStream;
        try {
            java.io.File yourFile = new java.io.File("insert SQL.txt");
            if (!yourFile.exists()) {
                yourFile.createNewFile();
            }

            fStream = new FileWriter("insert SQL.txt", true);
            sql = "INSERT INTO files (evento_id, user_id, path, preview, thumb, md5, width, height, created, modified) "
                    + "VALUES ("+file.getEventId()+","+file.getUserId()+",'"+file.getPath()+"','"+file.getPreview()+"','"+file.getThumb()+"','"+file.getMd5()+"',"
                            +"',"+file.getWidth()+","+file.getHeight()+",NOW(),NOW());";
            fStream.append(sql);
            fStream.append(System.getProperty("line.separator"));
            fStream.flush();
            fStream.close();
        } catch (Exception ex) {
        }
        return false;
    } finally {
        close();
    }

    return true;
   }


/**
 * 
 */
private void close() {
    try { if (rset != null) rset.close(); } catch (Exception e) {};
    try { if (psment != null) psment.close(); } catch (Exception e) {};
    try { if (sment != null) sment.close(); } catch (Exception e) {};
}
}

0 个答案:

没有答案