Home >Database >Mysql Tutorial >Java连接SQLServer2012数据库

Java连接SQLServer2012数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:46:081209browse

欢迎进入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数据库

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn