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#与Java之异同
    MultiThread WebRequest : Timeout
    FCL(2) :: CollectionBase & Collection
    msn 81000306 无法登录,解决方法
    接口一个被我忽略的地方接口重定向技术
    <<Clr Via C#>> Key Points (1) (46章)
    FCL(3):: ArrayList & List (1)
    TCP保活机制
    子网划分
    freopen函数
  • 原文地址:https://www.cnblogs.com/wtiancai/p/123945.html
Copyright © 2011-2022 走看看