通過網站訪問數據庫是任何基於Web的開發的重要組件之一。 JDBC,一種允許Java與數據庫交談的機制。
Java數據庫連接(JDBC)是一種標準的應用程序編程接口(API),用於訪問數據庫,而不考慮應用程序驅動程序和數據庫產品。換句話說,JDBC爲數據庫提供了一個統一的接口,但是,如果更改數據庫管理系統和應用程序,則只需更改其驅動程序。 JDBC爲各種SQL數據庫和其他表格數據源(如電子表格或平面文件)提供跨DBMS連接。
下面是示例例如:
import java.sql.*;
class DBQuery1{
public static void main(String args[]) throws SQLException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){};
Connection cnn;
Statement mystmt;
ResultSet myrs;
String op = "jdbc:odbc:JavaTest";
cnn = DriverManager.getConnection(op,
"Admin", "");
mystmt = cnn.createStatement();
String sql;
sql = "SELECT * FROM SupplierMaster " +
"WHERE SupplierCode IN (" +
"SELECT SCode " +
"FROM Relation " +
"WHERE PCode IN (" +
"SELECT ProductCode " +
"FROM ProductMaster " +
"WHERE ProdCatg IN (" +
"SELECT CatgID " +
"FROM CategoryMaster " +
"WHERE CategoryName = 'Eatables')))";
myrs = mystmt.executeQuery(sql);
System.out.println(" "+"Sup Code" + " " +"Sup Name" + " " + "Sup Address ");
System.out.println("--------------------------------------------------------------------");
String name, add;
int code;
while (myrs.next())
{
code = myrs.getInt("SupplierCode");
name = myrs.getString("SupplierName");
add = myrs.getString("SupplierAddress");
System.out.println(" " + code +" " + name+" " + add);
}
}
}
你可以找到這個鏈接的更多信息:
http://webserver.ignou.ac.in/virtualcampus/adit/course/cst302/block2/cst302-bl2-u3.htm