con =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school","root", "abcd"); //school是数据库名,abcd是root账号的密码。
Statement stmt; //创建声明
stmt = con.createStatement(); //新增一条数据
stmt.executeUpdate("INSERTINTO user (username, password) VALUES ('qgao', '123456')");
ResultSet res =stmt.executeQuery("select LAST_INSERT_ID()");
int ret_id;
if (res.next()) {
ret_id = res.getInt(1);
System.out.print(ret_id);
}
//删除一条数据
String sql = "DELETE FROMuser WHERE username='Li Si'";
long deleteRes =stmt.executeUpdate(sql);
//如果为0则没有进行删除操作,如果大于0,则记录删除的条数
System.out.print("DELETE:" + deleteRes);
//更新一条数据
String updateSql = "UPDATEuser SET password = '1234' WHERE username='Gao Shou'";
long updateRes =stmt.executeUpdate(updateSql);
System.out.print("UPDATE:" + updateRes);
//查询数据并输出
String selectSql = "SELECT *FROM user";
ResultSet selectRes =stmt.executeQuery(selectSql);
while (selectRes.next()) { //循环输出结果集
String username =selectRes.getString("username");
String password =selectRes.getString("password");
System.out.print("\r\n\r\n");
System.out.print("username:" + username +"password:" + password);
}
} catch (Exception e) {
System.out.print("MYSQLERROR:" + e.getMessage());
}
}
}
--------------------------------------分割线 --------------------------------------
Ubuntu 14.04下安装MySQL
Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
--------------------------------------分割线 --------------------------------------