zoukankan      html  css  js  c++  java
  • 简单的数据库连接

    package Demo;

    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    import java.sql.*;
    public class printData {
      public printData() {
      }

      public static void main(String[] args) {
        Connection con;
        Statement sql;
        ResultSet rs;
        try{
          try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          }
          catch (ClassNotFoundException ex1) {
            ex1.printStackTrace();
          }
          con = DriverManager.getConnection("jdbc:odbc:test","sa","");
          sql = con.createStatement();
          rs = sql.executeQuery("select top 10 * from customers");
          while (rs.next()){
            System.out.print(rs.getString("CustomerID"));
            System.out.println();
          }
          con.close();
        }
        catch(SQLException ex){
          ex.printStackTrace();
        }
      }
    }




    注:
    如果不加上Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    则会提示出错
    java.sql.SQLException: No suitable driver

     at java.sql.DriverManager.getConnection(DriverManager.java:532)

     at java.sql.DriverManager.getConnection(DriverManager.java:171)

     at Demo.printData.main(printData.java:21)


    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  //加上这一句
    String url = "jdbc:odbc:student";
    Connection conn = DriverManager.getConnection(url,"","");

    连接前先注册驱动程序

  • 相关阅读:
    【C++和C#的区别杂谈】后自增运算符的结算时机
    个人作业——软件工程实践总结&个人技术博客
    Unity常见的三种数据本地持久化方案
    C++的逗号运算符
    米哈游--2020春招实习
    厦门飞鱼科技--2020春招实习
    tap4fun(成都尼必鲁)--2020春招实习
    腾讯IEG--2020春招实习
    吉比特&雷霆游戏--2020春招实习
    Docker 基础知识
  • 原文地址:https://www.cnblogs.com/wtiancai/p/123945.html
Copyright © 2011-2022 走看看