Java核心代码例程之:JDBC.java

发表于:2007-06-22来源:作者:点击数: 标签:
import java . sql .*; /** *Simpledemoofhowtoperforma SQL queryusingJDBC. **/ publicclassJDBC { publicstaticvoidmain(Stringargs[])throwsException { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connectionconn=DriverManager.getConnection(jdbc

   
import java.sql.*;

/**
 * Simple demo of how to perform a SQL query using JDBC.
 **/
public class JDBC
{

   public static void main(String args[]) throws Exception
   {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      
      Connection conn = DriverManager.getConnection("jdbc:odbc:EmpPrjDB", "admin", "letmein");
      Statement  stmt = conn.createStatement();
      ResultSet  rslt = stmt.executeQuery("SELECT name, city FROM employee");

      while (rslt.next())
      {
          System.out.println("Hello World from "
                            + rslt.getString(1) // You can use getString(columnNumber)
                            + " in " 
                            + rslt.getString("city")); // Or, you can use getString(columnName)
      }

      rslt.close();
      stmt.close();
      conn.close();
   }
}

原文转自:http://www.ltesting.net