The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          package jdbc;
           
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.Statement;
           
          public class Test {
              private Connection conn = null;
           
               public Test() {
                super();
               }
           
               public void getConnection() {
                try {
                 Class
                  .forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                  .newInstance();
                 String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=testDB";
                 String USER = "sa"; // 根據你自己設置的數據庫連接用戶進行設置
                 String PASSWORD = "123"; // 根據你自己設置的數據庫連接密碼進行設置
                 conn = DriverManager.getConnection(URL, USER, PASSWORD);
                } catch (java.lang.ClassNotFoundException ce) {
                 System.out.println("Get Connection error:");
                 ce.printStackTrace();
                } catch (java.sql.SQLException se) {
                    System.out.println("Get Connection error:");
                    se.printStackTrace();
                } catch (Exception e) {
                    System.out.println("Get Connection error:");
                    e.printStackTrace();
                }
               }
           
               public void testConnection() {
                if (conn == null)
                 this.getConnection();
                try {
                 String sql = "SELECT * FROM user";
                 Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(sql);
                 while (rs.next()) {
                     System.out.print(rs.getString("ID")+" ");
                     System.out.print(rs.getString("Name")+" ");
                     System.out.println(rs.getString("Email"));
                 }
                 rs.close();
                 stmt.close();
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                } finally {
                 if (conn != null)
                  try {
                   conn.close();
                  } catch (SQLException e) {
                  }
                }
               }
               public static void main(String[] args) {
                Test bean = new Test();
                bean.testConnection();
               }
          }
          運行程序,沒啥意外的話應該就OK了。連接代碼與SQLServer2000的有所不同。這兩句可以記下來備用~
          Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”).newInstance();
          String URL = “jdbc:sqlserver://localhost:1433;DatabaseName=數據庫名稱”;
          posted on 2010-07-21 16:39 Eric_jiang 閱讀(179) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 灵璧县| 宁武县| 侯马市| 周至县| 翼城县| 丹阳市| 阜平县| 青铜峡市| 东丰县| 安丘市| 临澧县| 辽中县| 庄浪县| 顺昌县| 平安县| 绩溪县| 垫江县| 桐柏县| 长寿区| 凉城县| 巩义市| 建湖县| 远安县| 米林县| 满城县| 崇左市| 惠水县| 西平县| 卫辉市| 康乐县| 织金县| 大姚县| 乌鲁木齐县| 定日县| 红安县| 九龙城区| 满城县| 浦北县| 吉安市| 枣阳市| 浦县|