>데이터 베이스 >MySQL 튜토리얼 >Java连接SQLServer2012数据库

Java连接SQLServer2012数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-07 15:46:081225검색

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 Java连接SQLServer2012数据库 import java.sql.*; public class Demo { public static void main(String[] args) { //声明变量 Connection con = null; Statement sta = null; ResultSet res = n

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

  Java连接SQLServer2012数据库

  import java.sql.*;

  public class Demo {

  public static void main(String[] args) {

  //声明变量

  Connection con = null;

  Statement sta = null;

  ResultSet res = null;

  try {

  //数据库地址

  String url = "jdbc:sqlserver://localhost:1433;databaseName=数据库名;";

  //用户名,密码

  String user = "sa";

  String password = "1234";

  //装载Class

  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

  //连接数据库

  con = DriverManager.getConnection(url, user, password);

  sta = con.createStatement();

  //执行SQL语句

  String sql = "SELECT * FROM Table";

  res = sta.executeQuery(sql);

  //

  while(res.next()){

  System.out.println(res.getString("Name")+"   "+res.getInt("Age")+"   "+res.getString("Sex"));

  }

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }finally{

  //关闭数据库连接

  if(res != null){

  res.close();

  }

  if(sta != null){

  sta.close();

  }

  if(con != null){

  con.close();

  }

  }

  }

  }

  使用sqljdbc4.jar

Java连接SQLServer2012数据库

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.