首页 > 其他分享 >2.1

2.1

时间:2024-02-28 23:12:31浏览次数:22  
标签:jdbc java String resultSet getObject sql 2.1

  1. 测试查询,成功输出查询结果
package com.jiuxiao.jdbc;

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

/**
 * 数据库连接测试
 *
 * @author WuDaoJiuXiao
 * @Date 2022/4/20 10:42
 * @since 1.0.0
 */
public class JdbcTest {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost:3306/people?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "0531";

        //1.加载驱动
        //mysql-connector-java 如果为 6.0 之上,需要用 com.mysql.cj.jdbc.Driver,之下为 com.mysql.jdbc.Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.建立连接
        Connection connection = DriverManager.getConnection(url, username, password);
        //3.向数据库发送 SQL 的对象 statement
        Statement statement = connection.createStatement();
        //4.执行语句
        String sql = "select * from student where age > 16";
        ResultSet resultSet = statement.executeQuery(sql);
        //5.输出结果
        while (resultSet.next()) {
            System.out.println("id : " + resultSet.getObject("id") + "\t"
                    + "name : " + resultSet.getObject("name") + "\t"
                    + "age : " + resultSet.getObject("age") + "\t"
                    + "password : " + resultSet.getObject("password") + "\t"
                    + "email : " + resultSet.getObject("email") + "\t"
                    + "tel : " + resultSet.getObject("tel") + "\t"
            );
        }
        //6.关闭连接,释放资源(一定要做)
        resultSet.close();
        statement.close();
        connection.close();
    }
}

image-20220420111056622

标签:jdbc,java,String,resultSet,getObject,sql,2.1
From: https://www.cnblogs.com/lml66/p/18042280

相关文章

  • 2.14
         ......
  • 2.15
     《人月神话》是一本经典的软件工程著作,作者是弗雷德里克·布鲁克斯。这本书首次出版于1975年,至今仍然对软件开发领域产生深远影响。在阅读这本书之后,我对软件工程的挑战和解决方案有了更深入的理解,并获得了许多启发和思考。 首先,书中强调了软件开发的复杂性。布鲁克斯指出,软......
  • 2024.2.14
    HomeView.vue<template><el-containerstyle="min-height:100vh"><el-asidewidth="sideWidth+'px'"style="background-color:rgb(255,255,255)"><!--width="sideWidth+'px'&......
  • 2.10
    <?xmlversion="1.0"encoding="utf-8"?><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"and......
  • 2.13
    packagecom.example.myapplication;importandroidx.annotation.Nullable;importandroidx.appcompat.app.AppCompatActivity;importandroid.annotation.SuppressLint;importandroid.app.AlertDialog;importandroid.content.DialogInterface;importandroid.conte......
  • 2.12
    ackagecom.example.myapplication;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.TextView;importjava.util.List;publi......
  • 2.11
    packagecom.example.myapplication;publicclasscostList{privateString_id;privateStringse;privateStringTitle;privateStringDate;privateStringMoney;publicStringgetMoney(){returnMoney;}public......
  • 2.14
    packagecom.example.myapplication;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.TextView;importjava.util.List;p......
  • 2.16 Android 手机端学习
    publicclassAccountAdapterextendsBaseAdapter{Contextcontext;List<AccountBean>mDatas;LayoutInflaterinflater;intyear,month,day;publicAccountAdapter(Contextcontext,List<AccountBean>mDatas){this.context=......
  • 2.17 Android 学习开发
    importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;importandroidx.fragment.app.Fragment;importandroidx.fragment.app.FragmentManager;importandroidx.fragment.app.FragmentPagerAdapter;importorg.jetbrains.annotations.NotNull;importja......