DDL、DML言語を使用してデータベースを基本的に操作します.
1540 ワード
nテーブルを作成し、データを挿入し、データを変更する.
import java.sql.Connection;
import java.sql.Statement;
public class CreateTable {
public static void main(String[] args) {
Connection con = null;
try {
//
con = DBCon .getConnectionFromPooledDataSource("jdbcPool/mydatasource");
//
Statement st = con.createStatement();
// SQL
String sql = "create table student(id int,name char(30),age int)";
// SQL
boolean b = st.execute(sql);
if (b) {
System.out.println("create success");
} else {
System.out.println("create fail");
}
// student
sql = "insert into student values(1,'andy',47)"
+ "insert into student values(2,'jacky',53)"
+ "insert into student values(3,' ',51)"
+ "insert into student values(4,' ',60)";
// SQL
b = st.execute(sql);
if (b) {
System.out.println("insert success");
} else {
System.out.println("create fail");
}
//
sql = "update student set name=' ' where id=1";
int rows = st.executeUpdate(sql);
// ,rows 1
if (rows > 0)
System.out.println("update success");
else
System.out.println("update fail");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}