import java.sql.*;
public class ConnSqlServer2005 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ConnSqlServer2005 sql = new ConnSqlServer2005();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Connection con;
private Statement stmt;
private ResultSet rs;
public ConnSqlServer2005() throws SQLException {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();//1295
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=search";
String name = "";
String password = "";
con = DriverManager.getConnection(url,name,password);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String str = "select * from news";
rs = stmt.executeQuery(str);
while (rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
/* rs = stmt.executeQuery("select max(id) from news");
* rs.next();
* int maxId = rs.getInt(1);
* maxId++;
* System.out.println("maxId:"+maxId);
* String insertSql = "insert into news(id,title,content)values("+maxId+",'新闻标题','新闻内容')"; boolean go = stmt.execute(insertSql); System.out.println("go="+go); */
} catch (Exception e) {
e.printStackTrace();
}
if (con != null) {
con.close(); }
if (stmt != null) {
stmt.close(); }
if (rs != null) {
rs.close(); }
}
}