package untitled3;
import java.sql.*;
//use pubs
//go
//create proc test
//@uid int
//as
//select * from titles where pub_id=@uid
public class Untitled2 {
//设定驱动程序,Microsoft SQL Server 2000 Driver for JDBC
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//指定SQL Server的URL和数据库名
static String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String sql = "select * from titles";
public Untitled2() {
try {
Class.forName(driver);
} catch (Exception ex) {
System.out.println("error......");
}
try {
Connection conn = DriverManager.getConnection(url, "sa", "");
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
String strSQL = "{call test(?)}";
CallableStatement cstmt = conn.prepareCall(strSQL);
cstmt.setInt(1, 1389);
ResultSet rs2 = cstmt.executeQuery();
System.out.println("===========================");
while (rs2.next()) {
System.out.println(rs2.getString(1));
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
Untitled2 untitled2 = new Untitled2();
}
}
import java.sql.*;
//use pubs
//go
//create proc test
//@uid int
//as
//select * from titles where pub_id=@uid
public class Untitled2 {
//设定驱动程序,Microsoft SQL Server 2000 Driver for JDBC
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//指定SQL Server的URL和数据库名
static String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String sql = "select * from titles";
public Untitled2() {
try {
Class.forName(driver);
} catch (Exception ex) {
System.out.println("error......");
}
try {
Connection conn = DriverManager.getConnection(url, "sa", "");
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
String strSQL = "{call test(?)}";
CallableStatement cstmt = conn.prepareCall(strSQL);
cstmt.setInt(1, 1389);
ResultSet rs2 = cstmt.executeQuery();
System.out.println("===========================");
while (rs2.next()) {
System.out.println(rs2.getString(1));
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
Untitled2 untitled2 = new Untitled2();
}
}