package com.zhaofei.jdbc.study;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class StatementTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StatementTest test = new StatementTest();
//test.createTable();
//test.insertTable();
//test.deleteTable();
//test.updateTable();
test.qureyTable();
}
public void createTable() {
DButil util = new DButil();
Connection conn = util.openConnection();
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "create table StuTbl(id int, name varchar(20), passwd varchar(20))";
stmt.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertTable() {
DButil util = new DButil();
Connection conn = util.openConnection();
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "insert into stutbl(id, name, passwd)values(1,'tom',20)";
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteTable() {
DButil util = new DButil();
Connection conn = util.openConnection();
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "delete from stutbl where id = 1";
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updateTable() {
DButil util = new DButil();
Connection conn = util.openConnection();
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "update stutbl set name='bigtom' where id =1";
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void qureyTable() {
DButil util = new DButil();
Connection conn = util.openConnection();
Statement stmt;
try {
stmt = conn.createStatement();
String sql = "select id, name from stutbl";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
System.out.println("id=" + id +" name=" + name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
学习_Java_Base_JDBC_Statement_示例
最新推荐文章于 2021-02-28 05:38:09 发布