博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库开发 - JDBC单元作业
阅读量:7167 次
发布时间:2019-06-29

本文共 2758 字,大约阅读时间需要 9 分钟。

  hot3.png

#JDBC 单元作业

输入图片说明

#解答:

##MySQL建立表格

CREATE TABLE `product` (`Id`  int NOT NULL AUTO_INCREMENT ,`ProductName`  varchar(100) NULL ,`Inventory`  int NULL ,PRIMARY KEY (`Id`));

##初始化表格数据

INSERT INTO `product` (`Id`, `ProductName`, `Inventory`) VALUES ('1', 'bread', '11');INSERT INTO `product` (`Id`, `ProductName`, `Inventory`) VALUES ('2', 'milk', '8');

##读取商品ID为1的商品记录,输出商品名称和库存数量

package com.hava.jdbc;import java.sql.*;/** * Created by yanfa on 2016/9/22. */public class ProductJDBC {    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";    static final String DB_URL = "jdbc:mysql://192.168.1.200/test";    static final String USER = "root";    static final String PASSWORD = "dVHJtG0T:pf*";    public static void findFirstOne() throws ClassNotFoundException {        Connection connection = null;        Statement statement = null;        ResultSet resultSet = null;        // 1. add Driver        Class.forName(JDBC_DRIVER);        // 2. create db connnection        try {            connection = DriverManager.getConnection(DB_URL,USER,PASSWORD);            // 3.run SQL            statement = connection.createStatement();            resultSet = statement.executeQuery("SELECT * FROM `product` WHERE `Id` = '1' LIMIT 0, 1000");            // 4.get userName            while(resultSet.next())            {                Integer index = resultSet.getRow();                Integer Id = resultSet.getInt("Id");                String ProductName = resultSet.getString("ProductName");                Integer Inventory = resultSet.getInt("Inventory");                System.out.println("resultSet [row]:" + index + " [product.Id]:" + Id + " [product.ProductName]:" + ProductName + " [product.Inventory]:" + Inventory);            }        } catch (SQLException e) {            // Exception            e.printStackTrace();        } finally {            try {                // 5. close connection                if(connection != null)                    connection.close();                if(statement != null)                    statement.close();                if(resultSet != null)                    resultSet.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}

##测试类

package com.hava.jdbc;import junit.framework.TestCase;/** * Created by zhanpeng on 2016/9/22. */public class ProductJDBCTest extends TestCase {    public void testFindFirstOne() throws Exception {        System.out.println("Class ProductJDBCTest Method testFindFirstOne");        ProductJDBC.findFirstOne();    }}

##打印输出

Class ProductJDBCTest Method testFindFirstOneresultSet [row]:1 [product.Id]:1 [product.ProductName]:bread [product.Inventory]:11Process finished with exit code 0

转载于:https://my.oschina.net/hava/blog/750404

你可能感兴趣的文章
Android项目导入Eclipse出现错误解决办法
查看>>
chromium开发准备——重定向webui资源
查看>>
Centos版Linux 一些常用操作命令
查看>>
开源数据访问组件Smark.Data 1.8
查看>>
K&R《C语言程序设计》代码慢录一
查看>>
jQuery去掉字符串起始和结尾的空格
查看>>
poj1572
查看>>
Boost.Lockfree官方文档翻译
查看>>
shell一些笔记
查看>>
挨踢江湖之十二
查看>>
asp.net c# repeater或gridview导出EXCEL的详细代码。
查看>>
移植rtmpdump(librtmp)到android
查看>>
android应用程序fps meter[帧数显示]的分析 —— 浅谈root的风险 (1)
查看>>
技术人生:我为什么要坚持写博客
查看>>
Unable to start T-SQL Debugging. Could not connect to the computer ‘.’
查看>>
Windows蓝屏dump文件查看器(转)
查看>>
Java多线程-线程的同步(同步代码块)
查看>>
nyoj322 sort 归并排序,树状数组
查看>>
leetcode -- Partition List
查看>>
timerTask任务定时器
查看>>