package com.xrom.ssh.test;
import java.sql.*;
import java.text.SimpleDateFormat;
/**
* 测试sql server 数据库是否能连接
* Created by bill xu on 2019/1/31.
*/
public class TestConn {
public static void main(String[] args) {
String DataBaseName = "mydb";//数据库名称
String user = "sa"; //数据库用户名
String Psd = "sa"; //数据库密码
String name = "localhost";//数据库ip地址
String instancename = "SQLEXPRESS";//实例名称
//实例连接方式
//如果式默认实例则不需要加实例名,直接这样jdbc:sqlserver://localhost;databaseName=mydb;user=sa;password=sa
//https://bbs.csdn.net/topics/392190826
String url = "jdbc:sqlserver://"+name +"instanceName="+instancename+
";databaseName="+DataBaseName+
";user="+user+";password="+Psd;//sa身份连接
//数据库端口连接方式
// String url = "jdbc:sqlserver://"+ name +":1433;databaseName="+DataBaseName+
// ";user="+user+";password="+Psd;//sa身份连接
// Declare the JDBC objects.
try {
// Establish the connection.
System.out.println("begin.");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println(url);
Connection conn = DriverManager.getConnection(url);
System.out.println("数据库连接成功!!!");
System.out.println("end.");
Statement stmt = null;
ResultSet rs = null;
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * FROM [Real]";
stmt = conn.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
// System.out.println("时间+" + rs.getTime(1));
// java.util.Date date;
// System.out.println("日期 +" + (date = rs.getDate(1)));
// SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// System.out.println("first:" + format0.format(date.getTime()));
// String timeStr = rs.getString(1);
// java.util.Date sene = format0.parse(timeStr);
// System.out.println("second:" + format0.format(sene.getTime()));
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
1
package com.xrom.ssh.test;
2
3
import java.sql.*;
4
import java.text.SimpleDateFormat;
5
6
/**
7
* 测试sql server 数据库是否能连接
8
* Created by bill xu on 2019/1/31.
9
*/
10
public class TestConn {
11
public static void main(String[] args) {
12
13
String DataBaseName = "mydb";//数据库名称
14
String user = "sa"; //数据库用户名
15
String Psd = "sa"; //数据库密码
16
String name = "localhost";//数据库ip地址
17
String instancename = "SQLEXPRESS";//实例名称
18
19
//实例连接方式
20
//如果式默认实例则不需要加实例名,直接这样jdbc:sqlserver://localhost;databaseName=mydb;user=sa;password=sa
21
//https://bbs.csdn.net/topics/392190826
22
String url = "jdbc:sqlserver://"+name +"instanceName="+instancename+
23
";databaseName="+DataBaseName+
24
";user="+user+";password="+Psd;//sa身份连接
25
26
//数据库端口连接方式
27
// String url = "jdbc:sqlserver://"+ name +":1433;databaseName="+DataBaseName+
28
// ";user="+user+";password="+Psd;//sa身份连接
29
30
// Declare the JDBC objects.
31
try {
32
// Establish the connection.
33
System.out.println("begin.");
34
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
35
System.out.println(url);
36
Connection conn = DriverManager.getConnection(url);
37
System.out.println("数据库连接成功!!!");
38
System.out.println("end.");
39
40
Statement stmt = null;
41
ResultSet rs = null;
42
43
// Create and execute an SQL statement that returns some data.
44
String SQL = "SELECT * FROM [Real]";
45
stmt = conn.createStatement();
46
rs = stmt.executeQuery(SQL);
47
48
49
// Iterate through the data in the result set and display it.
50
while (rs.next()) {
51
// System.out.println("时间+" + rs.getTime(1));
52
// java.util.Date date;
53
// System.out.println("日期 +" + (date = rs.getDate(1)));
54
// SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
55
// System.out.println("first:" + format0.format(date.getTime()));
56
// String timeStr = rs.getString(1);
57
// java.util.Date sene = format0.parse(timeStr);
58
// System.out.println("second:" + format0.format(sene.getTime()));
59
60
System.out.println(rs.getString(1) + " " + rs.getString(2));
61
}
62
63
conn.close();
64
65
}
66
catch (Exception e) {
67
e.printStackTrace();
68
}
69
}
70
}
71