wenchunl

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

前提条件:

  1.安装eclipse,mysql.java jdk

  2.安装mysql connect J  (我安装的版本是mysql connect J 5.1.39)

  3.配置java环境变量

  4.使用mysql 创建schema,然后创建表,插入数据。(我创建的表:table1,字段:id,name)

使用eclipse编写代码:

  1. 新建包

  2. 新建类(勾选main选项,以便程序能执行)

  3. 完整的代码如下:

package com.ibm.reskill.java.jdbc_sample;


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

public class JdbcSample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Connection conn=null;
        
        //connect to the database
        try
        {
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema?useSSL=false","root","wen_201611!");
                        
            if (conn!=null) System.out.println("connect success");
            else
                System.out.println("connect failed");
            
        } catch (SQLException ex)
        {
            System.out.println("SQLException: xxx "+ex.getMessage());
            System.out.println("SQLState: "+ex.getSQLState());
            System.out.println("VendorError: "+ex.getErrorCode());
        }
        
        Statement stmt=null;
        ResultSet rs=null;
        
        try {
            stmt=conn.createStatement();
            rs=stmt.executeQuery("SELECT * FROM table1");
            
            while (rs.next())
            {
                System.out.println("The value of row "+rs.getRow()+" col 1 is "+rs.getInt(1));
                System.out.println("The value of row "+rs.getRow()+" col 2 is "+rs.getString(2));
            }
            
        } catch (SQLException ex)
        {
            System.out.println("SQLException: "+ex.getMessage());
            System.out.println("SQLState: "+ex.getSQLState());
            System.out.println("VendorError: "+ex.getErrorCode());
        }
        finally
        {
            if (rs!=null)
            {
                try{
                    rs.close();
                    System.out.println("\nrs destoryed");
                } catch (SQLException sqlEx) {}
                rs=null;
            }
            
            if (stmt!=null)
            {
                try{
                    stmt.close();
                    System.out.println("stmt destoryed");
                } catch (SQLException sqlEx){}
                stmt=null;
            }
        }
    }
}

注意事项:

如果出现找不到类的异常,尝试修改当前项目的java build path属性,把jdbc驱动文件加载进来。

 

posted on   良哥  阅读(1229)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
 
点击右上角即可分享
微信分享提示