zoukankan      html  css  js  c++  java
  • 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();
    		}
    	}
    
    }
    

      

  • 相关阅读:
    POJ 1995
    POJ 3233
    HDU 2815
    POJ 2417
    POJ 3243
    HDU 3579 线性同余方程组
    HDU 1573
    POJ 2115
    POJ 2891
    HDU 2035 不忍直视的水
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2152681.html
Copyright © 2011-2022 走看看