Java methods to manipulate Mysql

  • 2020-04-01 03:40:02
  • OfStack

This article illustrates how Java operates on Mysql. Share with you for your reference. Specific analysis is as follows:

Different from C# to manipulate the database, if Java is to manipulate the database statement into two:

There are select statements with results and insert into,update,delete statements with no results

1. If it's a select statement with a result, you need to define a ResultSet variable to fetch it, using con.preparestatement (SQL).executequery (); To query, where con is a database connection variable, in the program header must introduce Java. SQL.

Such as:


public String execute() { 
  String sql = "select * from a where username='" + username + "'"; 
    try { 
      rs = con.prepareStatement(sql).executeQuery(); 
      if (!rs.next()) { 
        // ...  
        con.close(); 
      } 
    } catch (Exception e) { 
      message = " Unable to connect to database! "; 
    } 
}

2. If there is no result of the insert into,update,delete and other statements, there is no need to define any variables, note that the query method is changed to:


con.createStatement().execute(sql);

Can be

I hope this article has been helpful to your Java programming.


Related articles: