1.下载jdbc:Download - JDBC Driver for SQL Server | Microsoft Docs
2.提取jdbc的内容:
3.导入jar包到eclipse
输入代码,运行:
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class cc { public static void main(String[] args) throws ClassNotFoundException, SQLException { // TODO Auto-generated method stub Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=AA;user=sa;password=123456;"; Connection conn = DriverManager.getConnection(connectionUrl); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM test"); //如果有数据,rs.next()返回true while(rs.next()){ System.out.println(rs.getString("ID")+" 年龄:"+rs.getInt("value")); } } }