|
|
|
@ -0,0 +1,29 @@
|
|
|
|
|
package com.example;
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
|
|
|
|
|
public class DBUtil {
|
|
|
|
|
private static String diver = "net.sourceforge.jtds.jdbc.Driver";
|
|
|
|
|
//加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况
|
|
|
|
|
private static String url = "jdbc:jtds:sqlserver://169.254.115.53:1433/exam;characterEncoding=UTF-8";
|
|
|
|
|
private static String user = "sa";//用户名
|
|
|
|
|
private static String password = "123";//密码
|
|
|
|
|
public static Connection getSQLConnection() {
|
|
|
|
|
Connection conn = null;
|
|
|
|
|
try {
|
|
|
|
|
Class.forName(diver);
|
|
|
|
|
conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|