import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
public class DBUtil {
private final static String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=canbinanalysis";//连接数据库的路径
private final static String userName = "sa";//数据库用户名
private final static String userPwd = "******";//数据库密码
private static Connection dbConn = null;
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);//连接数据库
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return dbConn;
}
public List<String> getSTring() throws SQLException {
Statement stmt = dbConn.createStatement();
ResultSet rs = stmt.executeQuery("select flight1,flight2 from Flight ");//执行sql语句
List<String[]> list1 = new ArrayList<String[]>();
List<String> list = new ArrayList<String>();
String start = null;
String arrive = null;
GregorianCalendar d = new GregorianCalendar();
for (int k = 0; k < 30; k++) {
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONDAY);
month = month +1 ;
int year = d.get(Calendar.YEAR);
Format f1 = new DecimalFormat("00");
Format f2 = new DecimalFormat("0000");
String date = f2.format(year) + f1.format(month) + f1.format(today);
d.add(Calendar.DAY_OF_MONTH, 1);
String url = "http://open.jiulvxing.com/open/av?start=START&arrive=ARRIVATE&date=DATE&sign=bjs&uuid=f6d0dd5d58e1491085994c5fdba814b6";
while (rs.next()) {
String[] s = new String[2];
s[0] = rs.getString("flight1");
s[1] = rs.getString("flight2");
list1.add(s);
}
for (int i = 0; i < list1.size(); i++) {
start = list1.get(i)[0];
arrive = list1.get(i)[1];
String u = url.replace("START", start).replace("ARRIVATE", arrive).replace("DATE", date);
list.add(u);
}
}
return list;
}
}
Java连接操作数据库
最新推荐文章于 2023-07-13 11:33:21 发布