在JDBC操作(1)文章中介绍了如何通过JDBC驱动连接mysql数据库。本文要介绍通过Java程序执行数据库的更新与查询语句。
首先要完成前期操作,在数据库'mydb'中创建表格'users'。
CREATE TABLE users(
u_id INT PRIMARY KEY AUTO_INCREMENT,
u_name VARCHAR(8) NOT NULL,
u_age INT NOT NULL,
u_sex VARCHAR(2) NOT NULL,
u_height FLOAT(4,1) NOT NULL,
birthday DATE NOT NULL
)ENGINE=INNODB DEFAULT CHARSET = utf8;
为了充分体现Java面向对象的特点,首先定义一个Jdbc类,之后就可以通过创建Jdbc对象来访问mysql数据库。
class Jdbc {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/";
private static final String USER = "root";
private static final String PASS = "****";
private Connection connection;
private Statement statement;
private String dbName;// 所要连接的数据库名称
public Jdbc(String dbName) {
this.dbName = dbName;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL + this.dbName, USER, PASS);
statement = connection.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public Statement getStatement() {
return statement;
}
public String getDbName() {
return dbName;
}
public void close() {
try {
if (statement != null)
statement.close();
} catch (Exception e) {
;
}
try {
if (connection != null)
connection.close();
} catch (Exception e) {
;
}
}
}
Statement接口中的 int executeUpdate(String sql) throws SQLException函数可执行INSERT,UPDATE,DELETE等sql语句。
一、执行数据库插入操作。
void accessDatabaseInsert() {
Jdbc myJdbc = new Jdbc("mydb");
try {
String sql = "INSERT INTO users(u_name,u_age,u_sex,u_height,birthday)VALUES"
+ "('赵阳','22','男','183.5','1994-08-17'),"
+ "('Jhone','21','女','175.0','1994-04-06')";
int count = myJdbc.getStatement().executeUpdate(sql);
System.out.println(count + "条记录成功插入");
} catch (Exception e) {
e.printStackTrace();
} finally {
myJdbc.close();
}
}
执行插入操作后,users表中新增了2条数据。
二、执行数据库修改操作。
void accessDatabaseUpdate() {
Jdbc myJdbc = new Jdbc("mydb");
try {
String sql = "UPDATE users SET birthday='1995-04-06' WHERE u_name='Jhone'";
int count = myJdbc.getStatement().executeUpdate(sql);
System.out.println(count+"成功修改");
} catch (Exception e) {
e.printStackTrace();
} finally {
myJdbc.close();
}
}
三、执行数据库删除操作。
void accessDatabaseDelete() {
Jdbc myJdbc =new Jdbc("mydb");
try{
int id = 1;
String sql="DELETE FROM users WHERE u_id="+id;
int count = myJdbc.getStatement().executeUpdate(sql);
System.out.println(count+"条记录被删除");
}
catch(Exception e) {
e.printStackTrace();
}
finally {
myJdbc.close();
}
}
四、执行数据库查询操作。
用Statement中的ResultSet executeQuery(String sql)函数来执行查询操作。ReseltSet接口用于接收用JDBC查询数据库得到的所有记录。ReseltSet中常用的方法有boolean next(),指向下一行记录。int getInt(int columnIndex)用属性的在数据表中的编号来获得整形属性值,int getInt(String columnName)用属性的名称来获得整形属性值。同理还有,float getFloat()、String getString()、Date getDate()。
void accessDatabaseQuery() {
Jdbc myJdbc = new Jdbc("mydb");
ResultSet result = null;
String sql = "SELECT u_id,u_name,u_age,u_sex,u_height,birthday FROM users";
try {
result = myJdbc.getStatement().executeQuery(sql);
while (result.next()) {
int id = result.getInt("u_id");
String name = result.getString("u_name");
int age = result.getInt("u_age");
String sex = result.getString("u_sex");
float height = result.getFloat("u_height");
java.util.Date birthday = result.getDate("birthday");
System.out.print("u_id: " + id);
System.out.print(" ,u_name: " + name);
System.out.print(" ,u_age: " + age);
System.out.print(" ,u_sex: " + sex);
System.out.print(" ,u_height: " + height);
System.out.println(" ,birthday: " + birthday);
}
result.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
myJdbc.close();
}
}
本文就介绍到这里。如有错误欢迎批评指正,谢谢!