One, the basic steps of using JDBC to operate the database
Second, the database
(1) Database foundation
1. Select statement (used to query data in the data table)
2. Insert statement (used to insert new data into the data table)
3. Update statement (used to modify the data in the data table)
4. Delete statement (used to Delete the data in the data table)
(2) Connect to the database
Common errors in connecting to the database:
3. Incorrect user name and password
4. Incompatible version of the driver package
Case Kangkang:
public class Demo7 {
public static void main(String[] args) {
Connection con = null;
Statement sm = null;
ResultSet rs = null;
try {
//加载驱动包
Class.forName("com.mysql.jdbc.Driver");
//建立连接
con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
//编写sql
String sql = "select * from user";
sm = con.createStatement();
rs = sm.executeQuery(sql);
while (rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println("编号:"+id+"\t"+"姓名:"+username+"\t"+"密码:"+password);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null) {
if (sm != null) {
if (con != null) {
try {
rs.close();
sm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
(3) Database query
1. Database query
2. Create interface object
3. Common methods of ResultSet
(4) Dynamic query
1. Is your SQL safe?
To prevent this SQL injection, JDBC provides dynamic query
2. Dynamic query
case study:
public class Demo7 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement sm = null;
ResultSet rs = null;
try {
//加载驱动包
Class.forName("com.mysql.jdbc.Driver");
//建立连接
con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
//编写sql
String sql = "select * from user where username like ?";
sm = con.prepareStatement(sql);
sm.setString(1,"小%");
rs = sm.executeQuery();
while (rs.next()){
System.out.println("姓名:"+rs.getString(2)+"\t密码:"+rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null) {
if (sm != null) {
if (con != null) {
try {
rs.close();
sm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
3. Case analysis of adding, modifying and deleting records
:
public class Demo7 {
public static void main(String[] args) {
Connection con = null;
Statement sm = null;
ResultSet rs = null;
try {
//加载驱动包
Class.forName("com.mysql.jdbc.Driver");
//建立连接
con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
/*插入数据*/
String sql = "insert into user(username,password) values('赵亮亮','123')";
sm = con.createStatement();
int i = sm.executeUpdate(sql);
System.out.println(i);
/*修改数据*/
String sql1 = "update user set username = '赵丹' where id = 10";
sm = con.createStatement();
int j = sm.executeUpdate(sql1);
System.out.println(j);
/*删除数据*/
String sql2 = "delete from user where id = 10 ";
sm = con.createStatement();
int k = sm.executeUpdate(sql2);
System.out.println(k);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null) {
if (sm != null) {
if (con != null) {
try {
rs.close();
sm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}