Java連結SqlServer資料庫

來源:互聯網
上載者:User

標籤:

 1 package Util; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 import org.omg.CORBA.PRIVATE_MEMBER;10 11 public class DBUtil {12     public static void main(String args[]) throws Exception {13         final String url = "jdbc:sqlserver://localhost:1433;databasename=stuDB";14         15         try {16             //1.註冊驅動17 //            Class.forName("com.mysql.jdbc.Driver");18             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");19             //2.獲得資料連結20             Connection conn = DriverManager.getConnection(url,"sa","123456");21             22             Statement stmt = conn.createStatement();23             ResultSet rs = stmt.executeQuery("select * from stuInfoTable");24     25             while(rs.next()) {26                 System.out.println(rs.getInt(1)+" "+ rs.getString(2)+" "+rs.getString(3));27             }28         } catch (ClassNotFoundException e) {29             // TODO Auto-generated catch block30             e.printStackTrace();31         }32     }33 }

 

Java連結SqlServer資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.