欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

6_7日

程序员文章站 2022-06-10 22:27:07
...

JDBC使用

6_7日

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();
    }
}

6_7日
6_7日
6_7日
6_7日

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;
    }
}
相关标签: jdbc