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

Java连接SQLServer2012数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:46:08
Original
1222 people have browsed it

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

Related labels:
Statement of this Website
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template