數(shù)據(jù)庫(kù)查詢代碼java 數(shù)據(jù)庫(kù)查詢代碼怎么寫

JAVA中怎么查詢代碼?

try{Connection con;

創(chuàng)新互聯(lián)長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寧河企業(yè)提供專業(yè)的網(wǎng)站制作、網(wǎng)站設(shè)計(jì)寧河網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

Statement stmt;

ResultSet rs;

int temp;

Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是數(shù)據(jù)庫(kù)連接,不同的數(shù)據(jù)管理器有 //不同的驅(qū)動(dòng)和鏈接方式,以上是mysql的連接

stmt=con.createStatement();

rs=stmt.executeQuery("select * from student");//執(zhí)行查詢語句,結(jié)果賦值給結(jié)果集rs

//結(jié)果集是結(jié)果于字段編號(hào)的映射,每一個(gè)字

//段都有一個(gè)編號(hào),最小為1,也就是第一個(gè)字段

while(rs.next()){

String names=rs.getString("name");//查詢結(jié)果轉(zhuǎn)換成字符串。

System.out.println(names);

}rs.close();

}catch(Exception e){

e.printStackTrace();

}

如何用Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢

import java.sql.*;

public class MSSQLText

{

public static void main(String args[])

{

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";

String user="sa";//這里替換成你自已的數(shù)據(jù)庫(kù)用戶名

String password="sa";//這里替換成你自已的數(shù)據(jù)庫(kù)用戶密碼

String sqlStr="select CustomerID, CompanyName, ContactName from Customers";

try

{ //這里的異常處理語句是必需的.否則不能通過編譯!

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

System.out.println("類實(shí)例化成功!");

Connection con = DriverManager.getConnection(url,user,password);

System.out.println("創(chuàng)建連接對(duì)像成功!");

Statement st = con.createStatement();

System.out.println("創(chuàng)建Statement成功!");

ResultSet rs = st.executeQuery(sqlStr);

System.out.println("操作數(shù)據(jù)表成功!");

System.out.println("----------------!");

while(rs.next())

{

System.out.print(rs.getString("CustomerID") + " ");

System.out.print(rs.getString("CompanyName") + " ");

System.out.println(rs.getString("ContactName"));

}

rs.close();

st.close();

con.close();

}

catch(Exception err){

err.printStackTrace(System.out);

}

}

}

一段java中數(shù)據(jù)庫(kù)代碼解釋

StringBuffer

sqlq=new

StringBuffer("

SELECT

*

FROM

")

;//申明一個(gè)可變字符串

,要存了一個(gè)sql語句,并且由"

SELECT

*

FROM

"可知其為一個(gè)select查詢語句

sqlq.append(DtoMapGroupOptions.DB_TABLE_NAME)

;//DtoMapGroupOptions.DB_TABLE_NAME應(yīng)該是一個(gè)字符串,字面值為一個(gè)表的名稱,要在這個(gè)表里查數(shù)據(jù)

sqlq.append("

ORDER

BY

")

;//這個(gè)制定查出來的結(jié)果集需要排序

sqlq.append(DtoMapGroupOptions.COLUMN_optionID)

;//DtoMapGroupOptions.COLUMN_optionID應(yīng)該是某一列的列名,根據(jù)這一列來排序,如果這一列是數(shù)字,那么就會(huì)根據(jù)數(shù)字大小排,字符串可能按abc排,和excel排序時(shí)一樣的,即根據(jù)某一列來擴(kuò)展至整個(gè)區(qū)域排序

sqlq.append("

DESC

")

;//這個(gè)事制定按降序還是升序,這里是降序

//后面的語句要看上下文,那個(gè)pb不知是什么

ListRow

list

=

null

;

pb.isRequireTotalRow(true);

String

sqlStr=sqlq.toString();

list

=

pb.getInfo(sqlStr,

null,

DtoMapGroupOptions.DATA_SOURCE_ID);//可能是把結(jié)果集放入list中,根據(jù)sqlStr中的sql語句

Java中的大量數(shù)據(jù)查詢

問題描述 在通常的三層構(gòu)架下 客戶通過Browser請(qǐng)求Web服務(wù)器查詢數(shù)據(jù)庫(kù) 而查詢結(jié)果是上千條甚至是上百萬條記錄 要求查詢結(jié)果傳送到客戶端瀏覽器并分頁顯示

考慮因素

Web服務(wù)器的資源消耗 包括 內(nèi)存(用來存儲(chǔ)查詢結(jié)果) 數(shù)據(jù)庫(kù)相關(guān)資源(數(shù)據(jù)庫(kù)連接對(duì)象 ResultSet對(duì)象等等)

DB服務(wù)器資源的消耗 包括游標(biāo) 會(huì)話等等

網(wǎng)絡(luò)開銷 包括與數(shù)據(jù)庫(kù)建立會(huì)話 傳輸查詢結(jié)果等等

JDBC中的幾個(gè)重要Class:

A ResultSet object maintains a cursor pointing to its current row of data Initially the cursor is positioned before the first row The next method moves the cursor to the next row and because it returns false when there are no more rows in the ResultSet object it can be used in a while loop to iterate through the result set

ResultSet是直接在數(shù)據(jù)庫(kù)上建立游標(biāo) 然后通過ResultSet的行位置定位接口來獲得指定行位置的記錄 當(dāng)用戶通過get方法獲取具體紀(jì)錄的內(nèi)容時(shí) ResultSet才從數(shù)據(jù)庫(kù)把所需數(shù)據(jù)讀到客戶端

Oracle的ResultSet實(shí)現(xiàn)似乎會(huì)在本地緩存用戶讀取過的數(shù)據(jù) 導(dǎo)致內(nèi)存消耗會(huì)隨讀取數(shù)據(jù)的增加而增加 這樣 如果一次查詢并讀取海量數(shù)據(jù) 即使讀出數(shù)據(jù)后馬上丟棄(比如直接寫入文件) 內(nèi)存消耗也會(huì)隨查詢結(jié)果的增加而遞增

The RowSet interface extends the standard java sql ResultSet interface A RowSet object may make a connection with a data source and maintain that connection throughout its life cycle in which case it is called a connected rowset A rowset may also make a connection with a data source get data from it and then close the connection Such a rowset is called a disconnected rowset A disconnected rowset may make changes to its data while it is disconnected and then send the changes back to the original source of the data but it must reestablish a connection to do so

RowSet是JDBC 中提供的接口 Oracle對(duì)該接口有相應(yīng)實(shí)現(xiàn) 其中很有用的是 oracle jdbc rowset OracleCachedRowSet OracleCachedRowSet實(shí)現(xiàn)了ResultSet中的所有方法 但與ResultSet不同的是 OracleCachedRowSet中的數(shù)據(jù)在Connection關(guān)閉后仍然有效

解決方案一 直接使用ResultSet來處理

從ResultSet中將查詢結(jié)果讀入collection 緩存在HttpSession或有狀態(tài)bean中 翻頁的時(shí)候從緩存中取出一頁數(shù)據(jù)顯示 這種方法有兩個(gè)主要的缺點(diǎn) 一是用戶可能看到的是過期數(shù)據(jù) 二是如果數(shù)據(jù)量非常大時(shí)第一次查詢遍歷結(jié)果集會(huì)耗費(fèi)很長(zhǎng)時(shí)間 并且緩存的數(shù)據(jù)也會(huì)占用大量?jī)?nèi)存 效率明顯下降

對(duì)上述方法的一種改進(jìn)是當(dāng)用戶第一請(qǐng)求數(shù)據(jù)查詢時(shí) 就執(zhí)行SQL語句查詢 獲得的ResultSet對(duì)象及其要使用的連接對(duì)象都保存到其對(duì)應(yīng)的會(huì)話對(duì)象中 以后的分頁查詢都通過第一次執(zhí)行SQL獲得的ResultSet對(duì)象定位取得指定頁的記錄(使用rs last();rs getRow()獲得總計(jì)錄條數(shù) 使用rs absolute()定位到本頁起始記錄) 最后在用戶不再進(jìn)行分頁查詢時(shí)或會(huì)話關(guān)閉時(shí) 釋放數(shù)據(jù)庫(kù)連接和ResultSet對(duì)象等數(shù)據(jù)庫(kù)訪問資源 每次翻頁都只從ResultSet中取出一頁數(shù)據(jù) 這種方式在某些數(shù)據(jù)庫(kù)(如oracle)的JDBC實(shí)現(xiàn)中差不多也是回緩存所有記錄而占用大量?jī)?nèi)存 同時(shí)速度也非常慢

在用例分頁查詢的整個(gè)會(huì)話期間 一個(gè)用戶的分頁查詢就要占用一個(gè)數(shù)據(jù)庫(kù)連接對(duì)象和結(jié)果集的游標(biāo) 這種方式對(duì)數(shù)據(jù)庫(kù)的訪問資源占用比較大 并且其利用率不是很高

優(yōu)點(diǎn) 減少了數(shù)據(jù)庫(kù)連接對(duì)象的多次分配獲取 減少了對(duì)數(shù)據(jù)庫(kù)的SQL查詢執(zhí)行

缺點(diǎn) 占用數(shù)據(jù)庫(kù)訪問資源-數(shù)據(jù)庫(kù)連接對(duì)象 并占用了數(shù)據(jù)庫(kù)上的資源-游標(biāo) 會(huì)消耗大量?jī)?nèi)存

解決方案二 定位行集SQL查詢

使用數(shù)據(jù)庫(kù)產(chǎn)品提供的對(duì)查詢的結(jié)果集可定位行范圍的SQL接口技術(shù) 在用戶的分頁面查詢請(qǐng)求中 每次可取得查詢請(qǐng)求的行范圍的參數(shù) 然后使用這些參數(shù)生產(chǎn)取得指定行范圍的的SQL查詢語句 然后每次請(qǐng)求獲得一個(gè)數(shù)據(jù)庫(kù)連接對(duì)象并執(zhí)行SQL查詢 把查詢的結(jié)果返回給用戶 最后釋放說有的數(shù)據(jù)庫(kù)訪問資源

這種方式需要每次請(qǐng)求時(shí)都要執(zhí)行數(shù)據(jù)庫(kù)的SQL查詢語句 對(duì)數(shù)據(jù)庫(kù)的訪問資源是使用完就立即釋放 不白白占用數(shù)據(jù)庫(kù)訪問資源 對(duì)特定(提供了對(duì)查詢結(jié)果集可定位功能的)的數(shù)據(jù)庫(kù)產(chǎn)品 如 Oracle(rowid或rownum ) DB (rowid或rownum ()) PostgreSQL(LIMIT 和 OFFSET) mySQL(Limit)等 (MS SQL Server 沒有提供此技術(shù) )

下面是在oracle下的查詢語句示例

SELECT * FROM ( SELECT row_ * rownum rownum_ FROM ( ) row_ WHERE rownum = {pageNumber*rowsPerPage}) WHERE rownum_ {(pageNumber )*rowsPerPage}

優(yōu)點(diǎn) 對(duì)數(shù)據(jù)庫(kù)的訪問資源(數(shù)據(jù)庫(kù)連接對(duì)象 數(shù)據(jù)庫(kù)游標(biāo)等)沒有浪費(fèi) 這些資源的充分重復(fù)的利用

lishixinzhi/Article/program/Java/hx/201311/25890

java連接數(shù)據(jù)庫(kù)的代碼

package mysql;

import java.sql.*;

/**

* @author xys

*/

public class ConnectMysql {

public static Connection getConnection() throws ClassNotFoundException, SQLException {

String url = "jdbc:mysql://localhost:3306/databaseName";

String user = "mysqluser";

String password = "password";

String driverClass = "com.mysql.cj.jdbc.Driver";

Connection connection = null;

Class.forName(driverClass);

try {

connection = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

if (connection != null) {

System.out.println("數(shù)據(jù)庫(kù)連接成功");

} else {

System.out.println("數(shù)據(jù)庫(kù)連接失敗");

connection.close();

}

return connection;

}

public void getResult() throws ClassNotFoundException, SQLException {

// 實(shí)例化 Statement 對(duì)象

Statement statement = getConnection().createStatement();

// 要執(zhí)行的 Mysql 數(shù)據(jù)庫(kù)操作語句(增、刪、改、查)

String sql = "";

// 展開結(jié)果集數(shù)據(jù)庫(kù)

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

// 通過字段檢索

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

// 輸出數(shù)據(jù)

System.out.println("ID : " +id);

System.out.println("name :" + name);

}

// 完成后需要依次關(guān)閉

resultSet.close();

statement.close();

getConnection().close();

}

}

java如何實(shí)現(xiàn)sql連接和查詢的代碼?

import java.sql.Connection。

import java.sql.DriverManager; ?

import java.sql.PreparedStatement; ?

import java.sql.ResultSet; ?

import java.sql.SQLException;

import javax.naming.Context; ?

import javax.naming.InitialContext; ?

import javax.naming.NamingException; ?

import javax.sql.DataSource;

public class DBCon {

//數(shù)據(jù)庫(kù)驅(qū)動(dòng)對(duì)象

public static final String DRIVER="oracle.jdbc.driver.OracleDriver";

//數(shù)據(jù)庫(kù)連接地址(數(shù)據(jù)庫(kù)名)

public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";

//登陸名

public static final String USER="FM";

//登陸密碼

public static final String PWD="FM";

//創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象

private Connection con=null;

//創(chuàng)建數(shù)據(jù)庫(kù)預(yù)編譯對(duì)象

private PreparedStatement ps=null;

//創(chuàng)建結(jié)果集

private ResultSet rs=null;

//創(chuàng)建數(shù)據(jù)源對(duì)象

public static DataSource source=null;

// ?//靜態(tài)代碼塊 ?

// ?static{ ?

// ?

// ? ? ?//初始化配置文件context ?

// ? ? ?try { ?

// ? ? ? ? ?Context context=new InitialContext(); ?

// ? ? ? ? ?source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage"); ?

// ? ? ?} catch (Exception e) { ?

// ? ? ? ? ?// TODO Auto-generated catch block ?

// ? ? ? ? ?e.printStackTrace(); ?

// ? ? ?} ?

// ?

// ?

// ?}

/**

* 獲取數(shù)據(jù)庫(kù)連接

*/

public Connection getCon(){

try {

Class.forName(DRIVER);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

con=DriverManager.getConnection(URL,USER,PWD);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return con;

} ?

// ?/** ?

// ? * 獲取數(shù)據(jù)庫(kù)連接 ?

// ? */ ?

// ?public Connection getCon(){ ?

// ?

// ? ? ?try { ?

// ? ? ? ? ?con=source.getConnection(); ?

// ? ? ?} catch (SQLException e) { ?

// ? ? ? ? ?// TODO Auto-generated catch block ?

// ? ? ? ? ?e.printStackTrace(); ?

// ? ? ?} ?

// ?

// ? ? ?return con; ?

// ?} ?

/**

* 關(guān)閉所有資源

*/

public void closeAll(){

if(rs!=null)

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(ps!=null)

try {

ps.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(con!=null)

try {

con.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} ?

}

/**

* @param sql數(shù)據(jù)庫(kù)更新(增、刪、改) 語句

* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)

* @return 返回受影響都行數(shù)

*/

public int update(String sql,String... pras){

int resu=0;

con=getCon();

try {

ps=con.prepareStatement(sql);

for(int i=0;ipras.length;i++){

ps.setString(i+1,pras[i]);

}

resu=ps.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

closeAll();

}

return resu;

}

/**

* @param sql數(shù)據(jù)庫(kù)查詢語句

* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)

* @return 返回結(jié)果集

*/

public ResultSet query(String sql,String... pras){

con=getCon();

try {

ps=con.prepareStatement(sql);

if(pras!=null)

for(int i=0;ipras.length;i++){

ps.setString(i+1, pras[i]);

}

rs=ps.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return rs;

} ?

}

分享文章:數(shù)據(jù)庫(kù)查詢代碼java 數(shù)據(jù)庫(kù)查詢代碼怎么寫
文章來源:http://www.muchs.cn/article10/doseodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)網(wǎng)站制作、Google、云服務(wù)器、網(wǎng)頁設(shè)計(jì)公司、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)