java 连接MSSQl

//加载驱动

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

//数据库地址

String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=hstudent";

 

//建立连接,参数1-数据库地址,参数2-用户名,参数3-密码

Connection conn = DriverManager.getConnection(url, "sa","");

String sql = "select * from student";//sql语句

//判断是否是查询

if(searchValue != null && searchValue.trim().length()>0)

     sql += " where name like ?";
}
//获得执行sql语句的Statement
PreparedStatement stmt = conn.prepareStatement(sql);
if(searchValue != null && searchValue.trim().length()>0)

     stmt.setString(1,"%" + searchValue + "%");
}

ResultSet rs = stmt.executeQuery();//执行查询

 

posted on 2008-11-26 19:50  黄洪汉  阅读(158)  评论(0编辑  收藏  举报