Java JDBC 查询数据



  • 查询数据

    考虑我们有下面一个MySQL表:
    CREATE TABLE `tutorials_tbl` (
      `tutorial_id` int(11) NOT NULL AUTO_INCREMENT,
      `tutorial_title` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
      `tutorial_author` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL,
      `submission_date` date DEFAULT NULL,
      PRIMARY KEY (`tutorial_id`)
    ) ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    
    表里面有三条数据:
    jdbc
    提示:不熟悉MySQL的同学可以到我们的MySQL教程进行学习。
    下面展示获取这三条条数据并展示的例子:
    import java.sql.*;
     
    
    public class MyClass {
            //驱动字符串
            final static String driverStr="com.mysql.cj.jdbc.Driver";
            final static String dbHost = "192.168.61.125";  // 主机  可为ip地址
            final static String dbUserName = "root";  // 用户
            final static String dbPassword = "123456"; // 密码
            final static String dbPort = "3306"; // 端口
            final static String dbName = "mydb"; // 数据库名
            final static String connStr="jdbc:mysql://"+ dbHost +":"+ dbPort +"/"+ dbName +"?useSSL=false&serverTimezone=UTC"; 
         
        
        public static void main(String[] args){
            MyClass obj = new MyClass();
            Connection conn = obj.connect();
            try {
                    String sqlString = "SELECT * FROM tutorials_tbl";
                    Statement st = conn.createStatement();
                    ResultSet rs = obj.qurey(st, sqlString);
                            obj.showRet(rs);
                    } catch (SQLException e) {
                            System.out.println("执行失败");
                            e.printStackTrace();
                    }
        }
        
        // 链接
        protected Connection connect () {
            Connection conn = null;
            try{
                  Class.forName(MyClass.driverStr);  // 加载驱动
                  conn = DriverManager.getConnection(MyClass.connStr,MyClass.dbUserName,MyClass.dbPassword); // 连接
                  System.out.println("连接成功");
                }catch(Exception ex){
                  System.out.println("连接失败");
                  System.out.println(ex.toString());
            }
            
            return conn;
            }
        
        // 查询
        protected ResultSet qurey(Statement stmt,String sql) {
            ResultSet rs = null;
            try {
                  rs = stmt.executeQuery(sql);
                  System.out.println("查询成功");
                } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  System.out.println("查询失败");
                  e.printStackTrace();
                }
            
            return rs;
        }
        
        // 遍历结果
        
        protected void showRet(ResultSet rs) {
            try {
                  while(rs.next()){
                    System.out.println(rs.getInt("tutorial_id")+"\t\t"+rs.getString("tutorial_title")+ "\t\t"+ rs.getString("tutorial_author") +"\t\t"+ rs.getString("submission_date"));
                  }
                } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
    
            }
    }
    
    提示:不懂编译的同学查看:数据库连接一章