📄 crud.java
字号:
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class CRUD {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// create();
read("as' or 1 or '");
// upate();
// delete();
// read();
}
static void delete() throws Exception {
String sql = "delete from user where id=2";
cdu(sql);
}
static void create() throws Exception {
String sql = "insert into user(name, age) values('name1', 23)";
cdu(sql);
}
static void upate() throws Exception {
String sql = "update user set name='new name' where id=1";
cdu(sql);
}
static void cdu(String sql) throws Exception {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
int i = st.executeUpdate(sql);
System.out.println("i=" + i);
} finally {
JdbcUtils.realse(rs, st, conn);
}
}
static void read(String name) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select id, name, age from user where name='" + name
+ "'";
sql = "select id, name, age from user where name=?";
System.out.println("sql:" + sql);
// st = conn.createStatement();
ps = conn.prepareStatement(sql);
ps.setString(1, name);
// rs = st.executeQuery(sql);
rs = ps.executeQuery();
while (rs.next()) {
System.out.print(rs.getInt("id") + "\t");
System.out.print(rs.getString("name") + "\t");
System.out.print(rs.getInt("age"));
System.out.println();
}
} finally {
JdbcUtils.realse(rs, ps, conn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -