Java uses JDBC to connect to MySQL database and insert values on Idea platform

public class file {

    public static final String url = "jdbc:mysql://localhost:3306/carinfo";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "";

    public Connection conn = null;

    public void DBHelper(){

        try {
            Class.forName(name);
            conn = DriverManager.getConnection(url, user, password);
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM msg2 WHERE VINID LIKE '-%'");
            while (rs.next()){

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        } catch (SQLException e) {
            e.printStackTrace ();
        }
    }

    public void close(){
        try {
            this.conn.close();
        } catch (SQLException e) {
            e.printStackTrace ();
        }
    }
}
 
 
try {
                        String sql="insert into dvalue(start_mileage,end_mileage) values(?,?)";//An exception will be thrown
                        PreparedStatement preStmt=conn.prepareStatement(sql);//conn is the conn of the previous program
                        preStmt.setLong (1, km);
                        preStmt.setLong (2, km);

                        preStmt.executeUpdate();
                        System.out.println("Insert into database successfully");
                    } catch (SQLException e) {
                        e.printStackTrace ();
                    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725374&siteId=291194637