?? dataaccess.java
字號:
/*-------------------- ATM取款模擬Java實現(xiàn)-----------------
*--------------------- write by 223小組-------------------
*---------------------------2006.4------------------------
*
*------ dataAccess(數(shù)據(jù)操作)類:包含用于數(shù)據(jù)庫的相關(guān)操作
*/
import java.sql.*;
import java.io.*;
public class dataAccess
{
private Connection conn;
private Statement stmt;
//----------------------------------------------
// 該方法用于鏈接數(shù)據(jù)庫
//----------------------------------------------
public dataAccess() //構(gòu)造函數(shù)設(shè)置連接數(shù)據(jù)庫
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String uri ="jdbc:mysql://localhost/atm" ;
String user="root";
String password="";
conn=DriverManager.getConnection(uri,user,password);
stmt=conn.createStatement();
}
catch(Exception e1)
{
System.out.println(e1.toString());
}
}
//----------------------------------------------
//該方法用于返回某一特定數(shù)據(jù)集
//----------------------------------------------
public ResultSet getData(String sqlstr)//返回查詢的數(shù)據(jù)集
{
try
{
ResultSet rs=this.stmt.executeQuery(sqlstr);
return rs;
}
catch(Exception e)
{
System.out.println(e.toString());
return null;
}
}
//----------------------------------------------
//該方法用于返回某一特定數(shù)據(jù)集的行數(shù)
//----------------------------------------------
public int getRowcount(String sqlstr)
{
int i=0;
try
{
ResultSet rs=this.getData(sqlstr);
while(rs.next()) i++;
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
return i;
}
}
//----------------------------------------------
//該方法用于執(zhí)行某一特定的無返回SQl語句
//----------------------------------------------
public boolean exeSql(String sqlstr)//執(zhí)行無返回的SQL語句
{
try
{
stmt.execute(sqlstr);
return true;
}
catch(Exception e)
{
System.out.println(e.toString());
return false;
}
}
//----------------------------------------------
//該方法用于延時
//----------------------------------------------
public void sleep(int second)
{
try
{
Thread th=new Thread();
th.sleep(second*1000);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
protected void finalize()//析構(gòu)函數(shù)
{
try
{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
catch(Exception e)
{}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -