Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 616|回复: 0

新手学java,连接Access数据库并进行简单的增删查更改遇到的

[复制链接]

该用户从未签到

发表于 2014-4-1 21:37:48 | 显示全部楼层 |阅读模式
5Java金币
本帖最后由 danceinstyle 于 2014-4-1 21:44 编辑

本人刚注册账号,悬赏不是很高,请详解!
我本来要用switch语句来获取用户要进行哪一种操作。但是输入要执行哪一个操作后程序直接结束了,不等我输入要增删查更的内容了。运行结果和Access数据库文件都在下面,还有Access数据库文件的截图,请看图
代码如下:
import java.io.IOException;
import java.sql.*;
class getConn {
    Connection connection;
    Statement statement;
    public getConn() {
        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection ("jdbcdbc:Student","","");
        statement = connection.createStatement();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
public class AccessDB {
    public static void main(String[] args) throws Exception{
        AccessDB adb = new AccessDB();
        byte[] operationOptions = new byte[1];
        System.out.println("请选择你要执行的操作。1.查询数据库所有的内容 2.插入记录  3.删除记录  4.更新记录");
        try {
            System.in.read(operationOptions);
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        String operations = new String(operationOptions);
        operations = operations.trim();
        int ope = Integer.parseInt(operations);
        switch (ope) {
            case 1:adb.getRecords();
                break;
            case 2:adb.insertRecords();
                break;
            case 3:adb.deleteRecords();
                break;
            case 4:adb.updateRecords();
                break;
        }
    }
    public void getRecords() {
        try {
            getConn con = new getConn();
            Connection c = con.connection;
            Statement stmt = con.statement;
            String sqlstr = "select * from student";
            int i = 1;
            ResultSet rs = stmt.executeQuery(sqlstr);
            while (rs.next()) {
                int num = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                System.out.println("第" + i + "条: " +num + "  " + name + "    " + age);
                i = i + 1;
            }
            stmt.close();
            c.close();
        } catch (Exception ex) {
                ex.printStackTrace();
        }
    }
    public void insertRecords() {
        byte[] b = new byte[255];
        System.out.println("请输入你要输入的记录,记录的格式为:\"学号 姓名 年龄\"");
        try {
            System.in.read(b);
        } catch (IOException iex) {
            iex.printStackTrace();
        }
        String userInput = new String(b);
        userInput = userInput.trim();
        String[] ui = userInput.split(" ");
        try {
            getConn con = new getConn();
            Connection c = con.connection;
            Statement stmt = con.statement;
            String sqlstr = "insert into student(num, name, age) values('" + Integer.parseInt(ui[0]) + "','" + ui[1] + "', '"
                + Integer.parseInt(ui[2]) +"')";
            stmt.executeUpdate(sqlstr);
            stmt.close();
            c.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("记录已插入,请查看!");
    }
    public void deleteRecords() {
        System.out.println("请输入你要删除的记录的学号:");
        byte[] b = new byte[255];
        try {
            System.in.read(b);
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        String userInput = new String(b);
        userInput = userInput.trim();
        String[] ui = userInput.split(" ");
            try {
                getConn con = new getConn();
                Connection c = con.connection;
                Statement stmt = con.statement;
                for (int i = 0; i <= ui.length-1; i++) {
                    stmt.executeUpdate("delete from student where num="+ui);
                }
                stmt.close();
                c.close();
                System.out.print("execute ok");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public void updateRecords() {
        try {
            getConn con = new getConn();
            Connection c = con.connection;
            Statement stmt = con.statement;
            String sqlstr ="update student set name='danceinstyle' where num=2";
            stmt.executeUpdate(sqlstr);
            stmt.close();
            c.close();
            System.out.println("更新成功!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}




Access%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%87%E4%BB%B6(344.00K)

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-2 21:20 , Processed in 0.510610 second(s), 48 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表