本文目錄一覽:
- 1、如何用Java實現資料庫查詢
- 2、java中怎麼把資料庫中數據查詢出來
- 3、用JAVA如何來查詢資料庫裡面相關的數據
- 4、java中,用DAO查詢一個資料庫步驟,分哪幾個步驟,原理解析
- 5、java 資料庫查詢某一條記錄
如何用Java實現資料庫查詢
import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url=”jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind”;
String user=”sa”;//這裡替換成你自已的資料庫用戶名
String password=”sa”;//這裡替換成你自已的資料庫用戶密碼
String sqlStr=”select CustomerID, CompanyName, ContactName from Customers”;
try
{ //這裡的異常處理語句是必需的.否則不能通過編譯!
Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”);
System.out.println(“類實例化成功!”);
Connection con = DriverManager.getConnection(url,user,password);
System.out.println(“創建連接對像成功!”);
Statement st = con.createStatement();
System.out.println(“創建Statement成功!”);
ResultSet rs = st.executeQuery(sqlStr);
System.out.println(“操作數據表成功!”);
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中怎麼把資料庫中數據查詢出來
剛剛漏了帳號密碼了,現在補上
try {
//這裡的是MYSQL 舉例
//載入驅動
Class.forName(“com.mysql.jdbc.Driver”);
//創建資料庫連接
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);
//創建查詢 「請求」
PreparedStatement ps = con.prepareStatement(“select * from user”);
//返回查詢結果
ResultSet rs = ps.executeQuery();
//遍歷結果
while(rs.next()) {
//假如 User 表中 有個 name 列
System.out.println(“name “+rs.getString(“name”));
}
//關閉
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
用JAVA如何來查詢資料庫裡面相關的數據
你的意思就是根據id
找數據本身以及他的葉子節點。
假設你的表叫location
rs
:
ResultSet
stmt:
Statement
public
ResultSet
getLocation(int
id)
{
String
sql
=
“select
id,
name,
pid
from
location
where
id
=
“
+
id
+
“or
pid
=
“
+
id;
rs
=
stmt.executeQuery(sql);
}
java中,用DAO查詢一個資料庫步驟,分哪幾個步驟,原理解析
創建一個以JDBC連接資料庫的程序,包含7個步驟:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName(“com.mysql.jdbc.Driver”) ;
}catch(ClassNotFoundException e){
System.out.println(“找不到驅動程序類 ,載入驅動失敗!”);
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例註冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=truecharacterEncoding=gbk ;
useUnicode=true:表示使用Unicode字符集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,
該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和
密碼來獲得。
例如:
//連接MySql資料庫,用戶名和密碼都是root
String url = “jdbc:mysql://localhost:3306/test” ;
String username = “root” ;
String password = “root” ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println(“資料庫連接失敗!”);
se.printStackTrace() ;
}
4、創建一個Statement
•要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3
種類型:
1、執行靜態SQL語句。通常通過Statement實例實現。
2、執行動態SQL語句。通常通過PreparedStatement實例實現。
3、執行資料庫存儲過程。通常通過CallableStatement實例實現。
具體的實現方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt =
con.prepareCall(“{CALL demoSp(? , ?)}”) ;
5、執行SQL語句
Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate
和execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句
,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或
DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的
語句。
具體實現的代碼:
ResultSet rs = stmt.executeQuery(“SELECT * FROM …”) ;
int rows = stmt.executeUpdate(“INSERT INTO …”) ;
boolean flag = stmt.execute(String sql) ;
6、處理結果
兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
• ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些
行中數據的訪問。
• 使用結果集(ResultSet)對象的訪問方法獲取數據:
while(rs.next()){
String name = rs.getString(“name”) ;
String pass = rs.getString(1) ; // 此方法比較高效
}
(列是從左到右編號的,並且從列1開始)
7、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲
明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連接對象
if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
java 資料庫查詢某一條記錄
1.可以用離線查詢,就是先把數據都取出來,再用list操作。
2.實時查詢,每次查詢5條記錄,即第一條,下一條,當前記錄,前一條,最後一條。這樣就有了每條記錄的id號,再提交按鈕的是後直接用java查詢,重複即可。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/151973.html