導航:首頁 > 編程語言 > java資料庫查詢

java資料庫查詢

發布時間:2022-02-10 07:38:49

『壹』 關於java連接資料庫查詢

String stuNumber=txtNumber.getText(); //得到文本框中你輸入學號學號

連接查詢:
PreparedStatement ps = null;

ResultSet rs = null;

Connection conn = DriverManager.getConnection(url,username,password);

ps = conn.prepareStatement("select name from student where number = ?");
ps.setString(1, stuNumber);
rs = ps.executeQuery();
while(rs.next()){
String stuName = rs.getString("name");
}

然後,把查出的姓名給文本框
txtName.setText(stuName);

『貳』 JAVA資料庫查詢功能實現

看你查詢語句是否與資料庫中有相匹配的值沒有,或在讀取值時是否正確。
反正集合中是空值,設斷點試試

『叄』 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怎麼將查詢到的資料庫的內容顯示到java圖形界面上

你查詢出來的結果在結合里, 比如叫rsif(rs.next){ //循環把你查詢的結果讀出來. 並填充到你新的窗體上}

『伍』 java資料庫查詢語句!

select a.部門,a.男,b.女 from
(

select 部門,員工人數 as 男 where 員工性別 = 男
) a
left outer join
(
select 部門,員工人數 as 女 where 員工性別 = 女
) b
on b.部門 = a.部門

『陸』 JAVA連接資料庫,查詢功能怎麼寫

public class JdbcNoarg {

//查詢一行數據

public void getQueryNoargOne(){

JdbcMysqlPool jdbc = new JdbcMysqlPool();

Connection con=null;

Statement st = null;

String sql;

ResultSet rst = null;

try {

//獲取資料庫連接

con = jdbc.getConnection();

//在連接里打開一條通道createStatement,返回Statement對象

st = con.createStatement();

sql = "select *from student";

//把我們想要結果sql語句發送給資料庫,資料庫返回的數據用java ResultSet來接收

rst= st.executeQuery(sql);

//獲取結果集的第一行數據9

rst.first();

//獲取結果集指定列的數據

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

String interest = rst.getString("interest");

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

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

if(rst!=null)

rst.close();

if(st!=null)

st.close();

if(con!=null)

con.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}


『柒』 JAVA SQL資料庫查詢

public String chaXun(String sname,String spass) throws Exception{
DBConnection dbc=new DBConnection(); /*資料庫的連接*/ Connection con=dbc.getConn(); /*資料庫的連接*/
String sql="select userpass1 from userDetail where username=?";/*SQL查詢語句:根據名字查詢*/
PreparedStatement prep=con.prepareStatement(sql);/*SQL查詢語句:根據名字查詢*/
prep.setString(1,sname);
ResultSet rs = prep.executeQuery();//得到一個根據name查詢回來的結果集,返回一行數據。
String result = "";
while(rs.next()){//判定rs中存在起碼一行數據
String pwd = rs.getString("password");//雙引號裡面寫的是資料庫里表示密碼的欄位。
result = pwd == spass?ture:flase; //如果pwd等於spass 返回true 否則false
}
return result;

『捌』 java中怎樣對mysql資料庫查詢

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.ResultSetMetaData;
importjava.sql.Statement;
importjava.util.Hashtable;


publicclassGetDBFiled{
publicstaticvoidconn(){
Stringdriver="com.mysql.jdbc.Driver";
Stringurl="jdbc:mysql://localhost:3306/databaseName";
Stringuser="root";
Stringpassword="密碼";
try{
Class.forName(driver);
Connectionconn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed()){
Stringsql="select*frompic";
System.out.println("!");
Statementstatement=conn.createStatement();
ResultSetrs=statement.executeQuery(sql);
if(rs.next()){
//得到ResultSetMetaData
ResultSetMetaDatarsmd=rs.getMetaData();
System.out.println(rsmd.getColumnCount());
for(inti=1;i<=rsmd.getColumnCount();i++){
Hashtablehst=newHashtable();
//把欄位名放入Name
Stringname=String.valueOf(rsmd.getColumnLabel(i));
hst.put("Name",name);
//把欄位類型放入Type
Stringtype=String.valueOf(rsmd.getColumnType(i));
hst.put("Type",type);
System.out.println(hst.get("Name")+""+hst.get("Type"));
}
}
}
}catch(Exceptione){

System.out.println("出現異常");
}

}
}

『玖』 java 在資料庫中如何查詢一條記錄

1.可以用離線查詢,就是先把數據都取出來,再用list操作。
2.實時查詢,每次查詢5條記錄,即第一條,下一條,當前記錄,前一條,最後一條。這樣就有了每條記錄的id號,再提交按鈕的是後直接用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資料庫查詢相關的資料

熱點內容
python組合數據類型題庫解析 瀏覽:76
電腦解壓文件的安裝包 瀏覽:467
不培訓能幹程序員嗎 瀏覽:281
編譯器怎麼分享微信 瀏覽:797
四川加密防塵網廠 瀏覽:284
列印機怎麼連上伺服器 瀏覽:618
2k20解壓後不能進去 瀏覽:190
伺服器掉線後顯示什麼 瀏覽:206
python根據經緯度獲取國家 瀏覽:47
stop伺服器有什麼作用 瀏覽:586
雲伺服器集群游戲伺服器 瀏覽:546
澪pro點伺服器閃退怎麼回事 瀏覽:855
同城砍票在APP哪裡找 瀏覽:574
c反匯編與逆向分析技術揭秘pdf 瀏覽:392
皮革pdf 瀏覽:221
utf8bomlinux 瀏覽:748
騰訊雲伺服器介面崩潰後自動重啟 瀏覽:436
mac開發pdf 瀏覽:320
本地sdk編譯開源代碼 瀏覽:820
伺服器是什麼等級 瀏覽:124