数据库查询代码:

public static void select() { Connection conn=null; Statement stmt=null; ResultSet rs=null; try { Class.forName(DB_DRIVER); showmsg("加载驱动完毕."); } catch (Exception e) { // TODO: handle exception showmsg(e.getMessage()); return; }

try {
    conn=DriverManager.getConnection(DB_URL, USER, PWD);
    stmt=conn.createStatement();
    String SQL="select * from tb3";
    long t1=System.currentTimeMillis();
    rs=stmt.executeQuery(SQL);
    long t2=System.currentTimeMillis();
    while(rs.next())
    {
        showmsg(rs.getInt(1)+" "+rs.getDouble(2)+" "+rs.getDate(3)+" "+rs.getString(4));
    }
    showmsg("select总共用时:"+(t2-t1)/1000+"s");
    rs.close();
    stmt.close();
} catch (Exception e) {
    // TODO: handle exception
    showmsg(e.getMessage());
}finally {
    try {
        conn.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
showmsg("goodby");  

}

数据库更新代码:

public static void update() { Connection conn=null; Statement stmt=null; try { Class.forName(DB_DRIVER); showmsg("加载驱动完毕."); } catch (Exception e) { // TODO: handle exception showmsg(e.getMessage()); return; }

try {
    conn=DriverManager.getConnection(DB_URL, USER, PWD);
    stmt=conn.createStatement();
    String SQL="update tb3 set name='UPDATED' where id=1";
    long t1=System.currentTimeMillis();
    stmt.executeUpdate(SQL);
    long t2=System.currentTimeMillis();
    showmsg("update总共用时:"+(t2-t1)/1000+"s");
    stmt.close();
} catch (Exception e) {
    // TODO: handle exception
    showmsg(e.getMessage());
}finally {
    try {
        conn.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
showmsg("goodby");  

}

数据库删除代码:

public static void delete() { Connection conn=null; Statement stmt=null; try { Class.forName(DB_DRIVER); showmsg("加载驱动完毕."); } catch (Exception e) { // TODO: handle exception showmsg(e.getMessage()); return; }

try {
    conn=DriverManager.getConnection(DB_URL, USER, PWD);
    stmt=conn.createStatement();
    String SQL="delete from tb3 where id=1";
    long t1=System.currentTimeMillis();
    stmt.executeUpdate(SQL);
    long t2=System.currentTimeMillis();
    showmsg("delete总共用时:"+(t2-t1)/1000+"s");
    stmt.close();
} catch (Exception e) {
    // TODO: handle exception
    showmsg(e.getMessage());
}finally {
    try {
        conn.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
showmsg("goodby");
public static void insert 	 TODO Auto-generated method stub Connection conn=null; Statement stmt=null; try ClassforNameDB_DRIVER; showmsg加载驱动完毕;

原文地址: https://www.cveoy.top/t/topic/eCRk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录