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();
    }
    }
    }
    }
    }

      

  • 相关阅读:
    jquery学习
    java--MVC引入JUnit单元测试
    BAE引擎发布到外网
    ORACLE1.26 综合:游标和动态SQL
    ORACLE1.25 动态SQL
    ORACLE1.24 银行系统操作和游标
    ORACLE1.23 loop,whild.for循环
    ORACLE1.23 if case when
    ORACLE1.22 %type %rowtype
    ORACLE1.21 PLSQL 01
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2132176.html
Copyright © 2011-2022 走看看