Java连接sqlserver2005

时间:2013-10-11 11:05:22
【文件属性】:

文件名称:Java连接sqlserver2005

文件大小:1KB

文件格式:JAVA

更新时间:2013-10-11 11:05:22

jdbc

import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCdemo { private final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=Test"; private static Connection conn = null; //获取连接对象 public static Connection getConn() { if (conn == null) { try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL,"sa","sasa"); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } //关闭资源 public static void closeALL(ResultSet rs, Statement stmt, Connection conn ){ try { if(rs != null) { rs.close(); rs = null; } if(stmt !=null){ stmt.close(); stmt = null; } if(conn != null){ conn.close(); conn = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }


网友评论

  • 不错,对于初学者
  • 此jdbc 连接数据库程序 运行成功 代码正确