zoukankan      html  css  js  c++  java
  • java 访问mysql 实例

    前提条件:

      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(" rs 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驱动文件加载进来。

  • 相关阅读:
    【转】编写高质量代码改善C#程序的157个建议——建议41:实现标准的事件模型
    【转】编写高质量代码改善C#程序的157个建议——建议40:使用event关键字为委托施加保护
    【转】编写高质量代码改善C#程序的157个建议——建议39:了解委托的实质
    【转】编写高质量代码改善C#程序的157个建议——建议38:小心闭包中的陷阱
    【转】编写高质量代码改善C#程序的157个建议——建议37:使用Lambda表达式代替方法和匿名方法
    7.FactoryBean 和BeanFactory去区别
    6.2-SingletonBeanRegistry-DefaultSingletonBeanRegistry
    6.1-AliasRegistry
    ConfigurableBeanFactory
    4.AutowireCapableBeanFactory 自动装配工厂
  • 原文地址:https://www.cnblogs.com/wenchunl/p/java_jdbc.html
Copyright © 2011-2022 走看看