jdbc for mssql2005 demo

package com.huawei.sqlserver;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class SqlTest {
    public static void main(String[] args) throws SQLException {
 
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
 
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(
                    "jdbc:sqlserver://localhost:1433;databaseName=test", "sa", "123");
            stmt = conn.prepareStatement("select * from T_User where id=?");
            stmt.setInt(1, 10);
            rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt("id"));
                System.out.println(rs.getString("name"));
                System.out.println(rs.getDate("birthday"));
                System.out.println("----------------------------");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        }
    }
 
}

  

posted @   水之原  阅读(230)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示