zoukankan      html  css  js  c++  java
  • JDBC for MSSql2005 简单示例

    package com.anllin.sqlserver;

    import java.sql.*;

    public class SqlserverTest
    {
    public static void main(String[] args)
    {
    sqlsserver2005Test();
    }

    public static void sqlsserver2005Test()
    {
    String connectionUrl
    = "jdbc:sqlserver://localhost:1433;databaseName=test;username=sa;password=123";

    Connection conn
    = null;
    Statement stmt
    = null;
    ResultSet rs
    = null;

    try
    {
    Class.forName(
    "com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn
    = DriverManager.getConnection(connectionUrl);
    conn.setAutoCommit(
    false);
    stmt
    = conn.createStatement();
    rs
    = stmt.executeQuery("select * from T_User");

    while (rs.next())
    {
    System.out.print(rs.getInt(
    "id") + " ");
    System.out.print(rs.getString(
    "name") + " ");
    System.out.println(rs.getDate(
    "birthday"));
    }

    conn.commit();
    }
    catch (Exception e)
    {
    if(null != conn)
    {
    try
    {
    conn.rollback();
    }
    catch (Exception e2)
    {
    e2.printStackTrace();
    }
    }
    }
    finally
    {
    if(null != rs)
    {
    try
    {
    rs.close();
    rs
    =null;
    }
    catch (Exception e2)
    {
    e2.printStackTrace();
    }
    }
    if(null != stmt)
    {
    try
    {
    stmt.close();
    stmt
    = null;
    }
    catch (Exception e2)
    {
    e2.printStackTrace();
    }
    }
    if(null != conn)
    {
    try
    {
    conn.close();
    conn
    =null;
    }
    catch (Exception e2)
    {
    e2.printStackTrace();
    }
    }
    }
    }
    }

      

  • 相关阅读:
    呵呵

    HDU 1878 欧拉回路
    HDU 3293 sort
    HDU 2714 ISBN
    神秀作偈
    大学之道
    写给自己过去疯狂的一年(2)(写在一个特别的时候)
    这几天我的生活就是这样的
    学习和研究计划
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2132176.html
Copyright © 2011-2022 走看看