diff --git "a/34 \345\210\230\346\231\272\347\277\224/.keep" "b/34 \345\210\230\346\231\272\347\277\224/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/34 \345\210\230\346\231\272\347\277\224/20230519 jsp\344\275\234\344\270\232.md" "b/34 \345\210\230\346\231\272\347\277\224/20230519 jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..2a79f38ae46990ef372577cc6aab093155ce72bb --- /dev/null +++ "b/34 \345\210\230\346\231\272\347\277\224/20230519 jsp\344\275\234\344\270\232.md" @@ -0,0 +1,168 @@ +```java +reate database student_db charset utf8; +use student_db; +create table studetn{ + id int, + name varchar(5), + sex varchar(2) + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + select * from student; +``` + +mysql链接增删改查 + +```java +import java.sql.*; + public class hhh { + private static final String url="jdbc:mysql:///test?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + static PreparedStatement ps=null; + static Connection conn=null; + static ResultSet re=null; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动有问题"); + e.printStackTrace(); + } + } + public static Connection conn() { + try { + conn = DriverManager.getConnection(url,username,password); + } catch (SQLException e) { + System.out.println("路径,用户名,密码错误请检查"); + } + return conn; + } + public static ResultSet select(String sql,String ...values){ + conn(); + ResultSet re = null; + try { + ps = conn().prepareStatement(sql); + for (int i = 0; i < values.length; i++) { + ps.setObject((i+1),values[i]); + } + re=ps.executeQuery(); + } catch (SQLException e) { + System.out.println("sql语句有问题"); + e.printStackTrace(); + } + return re; + } + public static int update(String sql,String ...values){ + conn(); + int i=0; + try { + ps= conn().prepareStatement(sql); + for (int a = 0; a < values.length; a++) { + ps.setObject((a+1),values[a]); + } + i=ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql语句有问题"); + e.printStackTrace(); + } + return i; + } + public static void close(){ + try { + ps.close(); + conn.close(); + re.close(); + } catch (SQLException e) { + System.out.println("无法关闭"); + e.printStackTrace(); + } + } + } +``` + +Student类 + +```java +public class Student { + int id; + String name; + String sex; + + public Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +mian方法测试 + +```java +import java.sql.*; +import java.util.ArrayList; + +public class test { + public static void main(String[] args) { + Connection conn=hhh.conn(); + ArrayList list=new ArrayList<>(); + String sql="select * from student"; + ResultSet re=hhh.select(sql); + Student stu=new Student(); + try { + while (re.next()){ + int id= re.getInt(1); + String name=re.getString(2); + String sex= re.getString(3); + stu.setId(id); + stu.setName(name); + stu.setSex(sex); + list.add(stu); + } + } catch (SQLException e) { + System.out.println("wuy1"); + } + System.out.println(list); + + } +} +``` \ No newline at end of file