java实现sql查询

java实现sql查询

 

package com.company.demo16mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class JdbcDemo {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con= DriverManager.getConnection("jdbc:mysql://192.168.10.128:3306/student?useUnicode=true&characterEncoding=UTF-8" ,"root","a12345");

//        String s = "张老师";
//        String sql = "SELECT * FROM course WHERE Tno = '张老师'";
        String sql = "SELECT * FROM course where Tno = "+s;
//        Statement statement = con.createStatement();
//        ResultSet rs = statement.executeQuery(sql);
//        int Cno = 0;
//        String Cname = null;
//        String Tno = null;
//        int a = 0;

//        ArrayList num = new ArrayList<>();
//        while (rs.next()) {
//            Cno = rs.getInt("Cno");
//
//            num.add(Cno);
//
//
//        }
//        System.out.println(Cno);
//        System.out.println(num);


//        String sql = name(1);
        String sql = "SELECT * FROM course where Tno = "+s;
//        Statement statement = con.createStatement();
//        ResultSet rs = statement.executeQuery(sql);
//        int Cno = 0;
//        String Cname = null;
//        String Tno = null;
//        int a = 0;
//
//        ArrayList num = new ArrayList<>();
//        while (rs.next()) {
//            Cno = rs.getInt("Cno");
//
//            num.add(Cno);
//
//
//        }
//        System.out.println(Cno);
//        System.out.println(num);


        for (int i = 0; i < 3; i++) {

            String sql = name(i);
//        String sql = "SELECT * FROM course where Tno = "+s;
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            int Cno = 0;
            String Cname = null;
            String Tno = null;
            int a = 0;

            String name3 =   name1().get(i);
           // ArrayList num = new ArrayList<>();
            while (rs.next()) {
//                Cno = rs.getInt("Cno");
//
//                num.add(Cno);
                Cno = rs.getInt("Cno");
                a+=Cno;

            }
            //System.out.println(a);
            System.out.println(name3+a);


        }



    }

    public static String  name(int cursor ){

        ArrayList name = new ArrayList<>();
        name.add("SELECT * FROM course WHERE Tno = '张老师'");
        name.add("SELECT * FROM course WHERE Tno = '王老师'");
        name.add("SELECT * FROM course WHERE Tno = '胡老师'");

        String te = name.get(cursor);
        return te;

    }
    public static ArrayList  name1( ){

        ArrayList name = new ArrayList<>();
        name.add("张老师");
        name.add("王老师");
        name.add("胡老师");

        return name;

    }

}

你可能感兴趣的:(java,mysql)