JAVA 连接MYSQL数据库操作
时间:2021-07-01 10:21:17
帮助过:16人阅读
连接字符串
String strUser = "数据库用户名";
//
String strPwd = "口令";
//"
System.out.println("正在连接数据库..."
);
try {
//监控异常
Class.forName("com.mysql.jdbc.Driver");
//加载驱动程序
Connection con;
//获得连接对象
con =
DriverManager.getConnection(strCon, strUser, strPwd);
System.out.println("成功连接到数据库。"
);
Statement sta = con.createStatement();
//创建语句对象
//执行SQL语句
String strSql = "select a.车次 ,a.列车类型,a.开车时间,b.到达时间,a.车站,b.车站,CONCAT(truncate((b.运行时间-a.运行时间)/60,0),‘小时‘,mod((b.运行时间-a.运行时间),60),‘分钟‘) as 运行时间,b.硬座-a.硬座+5 as 硬座 from (select * from sjc2222 where 车站 like ‘%"+textField_3.getText()+"%‘)a,(select * from sjc2222 where 车站 like ‘%"+textField_4.getText()+"%‘)b where a.车次 = b.车次 and a.站序 < b.站序 order by a.开车时间 asc"
;
ResultSet rs=
sta.executeQuery(strSql);
while(rs.next()){
Vector v =
new Vector ();
v.add(rs.getString("a.车次"
));
v.add(rs.getString("a.列车类型"
));
v.add(rs.getString("a.开车时间"
));
v.add(rs.getString("b.到达时间"
));
v.add(rs.getString("a.车站"
));
v.add(rs.getString("b.车站"
));
v.add(rs.getString("运行时间"
));
v.add(rs.getString("硬座"
));
dtm1.addRow(v);
System.out.println(v);
}
System.out.println(table_1.getRowCount());
if(table_1.getRowCount() == 1
){
table_1.setVisible(false);
}else{
table_1.setVisible(true);
}
rs.close();sta.close(); con.close();
} catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); }
catch (SQLException sqle) { sqle.printStackTrace(); }
JAVA 连接MYSQL数据库操作
标签:成功 state mysql 执行sql 执行 order str textfield and