java當(dāng)中JDBC當(dāng)中請(qǐng)給出一個(gè)sql server的stored procedure例子
sql server的stored procedure例子:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
import java.sql.*;
public class StoredProc0 {
public static void main(String[] args) {
String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/qixytest";
String user = "sa";
String password = "";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, user, password);
System.out.println("Connection is ok");
CallableStatement cs = con.prepareCall("{call all_books}");
ResultSet rs = cs.executeQuery();
while(rs.next()){
System.out.println(rs.getString("BookName") + "\t" +
rs.getString("Authorname") + "\t" +
rs.getInt("numbersold"));
}
rs.close();
cs.close();
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
第二個(gè)例子:
import java.sql.*;
public class StoredProc {
public static void main(String[] args) {
String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/qixytest";
String user = "sa"; String password = "";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, user, password);
System.out.println("Connection is ok");
CallableStatement cs = con.prepareCall("{call getInStockByBookName(?,?)}");
cs.registerOutParameter(2,Types.INTEGER);
cs.setString(1,"core java");
/*qixy: note execute,executeQuery and executeUpdate are roughly the same, the only
difference lies in their return value. Documentation: the "execute" method handles
these complex statements as well as the simpler form of statements handled by the
methods executeQuery and executeUpdate.
public ResultSet executeQuery() Executes the SQL query in this PreparedStatement
object and returns the ResultSet object generated by the query.
public int executeUpdate() Executes the SQL statement in this PreparedStatement
object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement
that returns nothing, such as a DDL statement.
Returns: either (1) the row count for INSERT, UPDATE, or DELETE statements or
(2) 0 for SQL statements that return nothing */
cs.execute(); System.out.println(cs.getInt(2));
cs.close(); con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}