mport java.sql.Connection;
創(chuàng)新互聯(lián)建站主營沛縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā)公司,沛縣h5小程序定制開發(fā)搭建,沛縣網(wǎng)站營銷推廣歡迎沛縣等地區(qū)企業(yè)咨詢
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ConnDB
{
public static void main(String[] args)
{
try
{
//我這里用mysql數(shù)據(jù)庫
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mytest";
Connection conn = DriverManager.getConnection(url, "root", "123");
String sql = "select * from user limit ?,?";//這里沒有括號(hào)
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, 1);//為問號(hào)賦值
ps.setInt(2, 3);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(2));
}
rs.close();
ps.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
希望對(duì)你有幫助
例示一個(gè)不帶參數(shù)的簡單存儲(chǔ)過程。多數(shù)存儲(chǔ)過程的功能比本例復(fù)雜多了,但這里主要說明存儲(chǔ)過程的一些基本要點(diǎn)。如前面所述,不同DBMS定義存儲(chǔ)過程的語法是不同的。例如,有些DBMS使用begin
.
.
.
end或其他關(guān)鍵字指明存儲(chǔ)過程定義的開始和結(jié)束。在有些DBMS中,下面的SQL語句可創(chuàng)建一個(gè)存儲(chǔ)過程:
create
procedure
SHOW_SUPPLIERS
as
select
SUPPLIERS.SUP_NAME,
COFFEES.COF_NAME
from
SUPPLIERS,
COFFEES
where
SUPPLIERS.SUP_ID
=
COFFEES.SUP_ID
order
by
SUP_NAME
下面的代碼將SQL語句放到一個(gè)字符串中,然后賦給變量createProcedure以備后用:
String
createProcedure
=
"create
procedure
SHOW_SUPPLIERS
"
+
"as
"
+
"select
SUPPLIERS.SUP_NAME,
COFFEES.COF_NAME
"
+
"from
SUPPLIERS,
COFFEES
"
+
"where
SUPPLIERS.SUP_ID
=
COFFEES.SUP_ID
"
+
"order
by
SUP_NAME";
下面的代碼段使用Connection對(duì)象con來創(chuàng)建Statement對(duì)象,用于把創(chuàng)建存儲(chǔ)過程的SQL語句發(fā)送給數(shù)據(jù)庫:
Statement
stmt
=
con.createStatement();
stmt.executeUpdate(createProcedure);
存儲(chǔ)過程SHOW_SUPPLIERS將作為一個(gè)可調(diào)用的數(shù)據(jù)庫對(duì)象在數(shù)據(jù)庫中編譯并存儲(chǔ),調(diào)用時(shí)就像調(diào)用其他方法一樣。
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// System.out.println("XOX");
con = DriverManager.getConnection(connectionUrl);
// System.out.println("XOX..");
String Uinfo = "SELECT UserName,UserPwd FROM Login";
// System.out.println("XXX");
stmt = con.createStatement();
// System.out.println("000");
rs1 = stmt.executeQuery(Uinfo);
你都寫到這了,下面就照著我說的寫唄。
一條記錄就
rs1.next();
Uname = rs1.getString(0);
Upwd = rs1.getString(1);
多條就
while(rs1.next()) {
Uname = rs1.getString(0);
Upwd = rs1.getString(1);
} 我說的很明白了吧。