Merge branch 'master' of http://bdgit.educoder.net/pnojz9at2/pv7q5cejw.git
commit
44d529a07a
@ -0,0 +1,80 @@
|
||||
package com.yuxue.test;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class sql{
|
||||
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
|
||||
+ "databaseName=AdventureWorks;integratedSecurity=true;";
|
||||
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=chepai;user=sa;password=197905";//以sa身份连接数据库:请根据自己的用户名和密码进行重新设置!!!!!!
|
||||
|
||||
// 声明JDBC对象
|
||||
Connection con = null;
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
// 建立数据库连接.
|
||||
System.out.println("尝试建立数据库连接.");
|
||||
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||
con = DriverManager.getConnection(url);
|
||||
System.out.println("数据库连接成功!");
|
||||
|
||||
// String SQL = "INSERT INTO Parking_in(number,color) VALUES('"+palte+"','"+col+"' )";
|
||||
// String SQL = "INSERT INTO Parking_out(number,color) VALUES('"+palte+"','"+col+"' )";
|
||||
stmt = con.createStatement();
|
||||
// stmt.executeUpdate(SQL);
|
||||
|
||||
String time1=null,time2=null;
|
||||
String SQL = "SELECT * FROM Parking_in";
|
||||
rs = stmt.executeQuery(SQL);
|
||||
if(rs.next())
|
||||
time1=rs.getString("time_in");
|
||||
System.out.println(time1);
|
||||
|
||||
|
||||
SQL = "SELECT * FROM Parking_out";
|
||||
rs = stmt.executeQuery(SQL);
|
||||
if(rs.next())
|
||||
time2=rs.getString("time_in");
|
||||
System.out.println(time2);
|
||||
|
||||
|
||||
Count count=new Count();
|
||||
double cost=count.charging(time1, time2);
|
||||
System.out.println(cost);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//处理和打印程序异常
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
finally {
|
||||
if (rs != null)
|
||||
try {
|
||||
rs.close(); //关闭查询结果集句柄
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (stmt != null)
|
||||
try {
|
||||
stmt.close(); //关闭语句句柄
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (con != null)
|
||||
try {
|
||||
con.close(); //关闭数据库连接
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue