JDBC:Java Database Connectivity(some homework)

1、establish a connection

Connection conn = DriverManager.getConnection("url","username","password");        [throws SQLException]

2、update table statement

Statement statement = conn.createStatement();

statement.executeUpdate(sqlNewTable);

3、Query a table

public void getQueryResult(){
    ResultSet rs=null;
    try {

Statement statement = mysqlConn.createStatement();
String sqlQuery="SELECT MODEL, PRICE FROM CAR WHERE MAKE='TOYOTA'“;

rs = statement.executeQuery(sqlQuery);

        while(rs.next()){

String MODEL = rs.getString(1);

Int PRICE  = rs.getInt(2);

System.out.println(MODEL  + ":$" + PRICE);

}

        } catch (SQLException ex) {

} }

4、Please explain the difference between a statement and a prepared statement in JDBC.

Statement has to compile once to execute an sql, and PrepareStatement only compiles once.

5、In the following code fragment, SQL statements will be sent and executed to the database management system for 3 times. Please modify the code fragment, so that we can put the 3 statements into a batch and execute them at one time.

statement.executeUpdate("SQL statement 1");

statement.executeUpdate("SQL statement 2");

statement.executeUpdate("SQL statement 3");

modify to :

statement.addbatch(""SQL statement 1");

statement.addbatch("SQL statement 2");

statement.addbatch("SQL statement 3");

statement.executebatch();

猜你喜欢

转载自blog.csdn.net/qq_42615643/article/details/85014126