6_7日
程序员文章站
2022-06-10 22:27:07
...
JDBC使用
public class JdbcDemo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/s_t?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" , "root", "ajkx19920lmd.+");
String sql = "update student set ssex = '男' where ssex = '汉' ";
Statement statement= conn.createStatement();
int count = statement.executeUpdate(sql);
System.out.println(count);
statement.close();
conn.close();
}
}
package com.Day6_7;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class JDBCDemo3 {
public static void main(String[] args) {
List<JDBCDemo2> list = new JDBCDemo3().FindAll();
System.out.println(list);
}
public List<JDBCDemo2> FindAll(){
List<JDBCDemo2> list = new ArrayList<JDBCDemo2>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/s_t?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "ajkx19920lmd.+");
Statement stmt = conn.createStatement();
String sql = "select * from student";
ResultSet res = stmt.executeQuery(sql);
while (res.next()){
String sno = res.getString("sno");
String sname = res.getString("sname");
String ssex = res.getString("ssex");
int sage = res.getInt("sage");
String sdept = res.getString("sdept");
JDBCDemo2 temp = new JDBCDemo2();
temp.setSno(sno);
temp.setSname(sname);
temp.setSsex(ssex);
temp.setSage(sage);
temp.setSdept(sdept);
list.add(temp);
}
stmt.close();
res.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return list;
}
}
上一篇: LeetCode-283. 移动零
下一篇: LeetCode编程题总结
推荐阅读