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();??}
?? }
??}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
