JDBC简单查询总结

http://www.shpe.icu/2021/04/10/JDBC数据库开发入门/

四大参数

创建dbconfig.properties

driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
url = jdbc:sqlserver://localhost:1433;DatabaseName=JXGL
username = sa
password = 123

JDBCUtils工具类

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static Properties props = null;

    //只在JDBCUtils类被加载时执行一次
    static {
        //给props进行初始化,即加载dbconfig.properties文件到props对象中
        try{
            //配置文件
            InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("dbconfig.properties");
            props = new Properties();
            props.load(in);
        }catch(IOException e){
            throw new RuntimeException(e);//异常处理
        }

        //加载驱动类
        try{
            Class.forName(props.getProperty("driverClassName"));
        }catch(ClassNotFoundException e){
            throw new RuntimeException(e);
        }
    }

    //获取连接!
    public static Connection getConnection() throws SQLException{
        //得到Connection
        return DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"),props.getProperty("password"));
    }
}

简单查询

import java.sql.*;

class Select {
    public static void main(String args[]) throws Exception{
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;//定义使用
        try {
            //加载驱动类
            con = JDBCUtils.getConnection();//实例化

            /*
             *二、创建Statement
             */
            stmt = con.createStatement();
            String sql = "Select * From 学生";
            rs = stmt.executeQuery(sql);

            /*
             *三、循环遍历rs,打印其中数据
             */
            int count = rs.getMetaData().getColumnCount();
            while(rs.next()){
                //遍历行
                for(int i=1;i<=count;i++){
                    //遍历列
                    System.out.print(rs.getString(i));
                    if(i<count)
                        System.out.print(",");
                }
                System.out.println();
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }finally{
            //关闭
            if(rs!=null) rs.close();
            if(stmt!=null) stmt.close();
            if(con!=null) con.close();
        }
    }
}

查询结果

D:\Code\JAVA\JXGL>javac -encoding UTF-8 Select.java

D:\Code\JAVA\JXGL>java Select
1,秦始皇,男,1999-06-24,070102,22
2,吕不韦,男,1999-05-20,070102,22
3,李斯,男,1998-06-21,070102,23
4,赵姬,女,2000-07-12,070102,21
5,庄襄王,男,1996-08-13,070102,25
6,李牧,男,1990-09-15,070102,31
7,王翦,男,1995-10-15,070102,26
8,蒙恬,男,1994-11-25,070102,27
9,孔子,男,1993-12-26,070102,28
10,孟子,男,1992-01-23,070102,29
11,西施,女,1991-02-24,070102,30
12,杨玉环,女,1992-03-11,070102,29
13,李白,男,1993-04-01,070102,28
14,韩愈,男,1994-05-28,070102,27
15,曹植,男,1995-06-23,070102,26
16,武则天,男,1996-07-02,070102,25
17,祝英台,男,1997-08-06,070102,24
18,梁山伯,男,1998-09-24,070102,23
19,武松,男,1999-10-24,070102,22
20,赵飞燕,女,1998-11-22,070102,23
21,李师师,女,1997-12-31,070102,24
22,张良,男,1994-11-22,070102,27
23,韩非,男,1995-12-02,070102,26
24,刘禹锡,男,1996-01-01,070102,25
25,王昭君,女,1995-10-21,070102,26
26,刘邦,男,1996-09-28,070102,25
27,项羽,男,1997-08-29,070102,24
28,樊哙,男,1998-07-24,070102,23
29,吕布,男,1999-06-23,070102,22
30,貂蝉,女,1995-05-22,070102,26
31,屈原,男,1994-04-21,070102,27
32,曹操,男,1993-02-20,070102,28
33,刘备,男,1997-02-04,070102,24
34,孙权,男,1998-05-14,070102,23
35,孙尚香,女,1999-07-24,070102,22
36,郑板桥,男,1994-05-05,070102,27
37,辛弃疾,男,1995-02-10,070102,26
38,比干,男,1992-04-06,070102,29
39,萧何,男,1991-02-07,070102,30
40,韩信,男,1999-06-30,070102,22
2004002,王刚,男,1994-01-01,计算机系,27
2004003,张三,男,1993-01-01,计算机系,28
posted @ 2021-04-11 22:49  -SuPer  阅读(207)  评论(0)    收藏  举报