java当中JDBC当中请给出一个sql server的stored procedure例子
sql server的stored procedure例子:
马克- to-win:马克 java社区:防盗版实名手机尾号: 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();
}
}
}
第二个例子:
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();
}
}
}