2012年2月22日星期三

PreparedStatement


Before running the following program,you should first create a table in a mssql database.

package liulixin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo2 {
public static void main(String[] args) {
try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {

Connection conn = DriverManager.getConnection(
"jdbc:sqlserver://*****.com;databaseName=****", "****",
"*****");

String sql ="select uName as userName  from tbl_user where uid=?";
PreparedStatement preStmt = conn.prepareStatement(sql);
preStmt.setInt(1, 2);
ResultSet rs=preStmt.executeQuery();
while(rs.next()){

System.out.print(rs.getString("userName")+"  " );
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

没有评论:

发表评论