概述import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConnect import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.sqlException; import java.sql.Statement; public class DBConnection { public static Connection getConn() { String drivername = "com.microsoft.sqlserver.jdbc.sqlServerDriver"; String user = ""; String password = ""; String dbname = ""; Connection con = null; String url = "jdbc:sqlserver:/**********:1433;databasename=" + dbname; try { Class.forname(drivername).newInstance(); System.out.println("*******开始进行
连接********"); con = DriverManager.getConnection(url,user,password); System.out.println("********连接数据库成功!***\n"); } catch (Exception e) { System.err.println("连接数据库不成功"); e.printstacktrace(); } return con; } /** * 关闭连接 * * @param conn * @param pre * @param rs */ public static voID closeConn(ResultSet rs,PreparedStatement pre,Connection conn) { try { if (conn != null) conn.close(); if (pre != null) pre.close(); if (rs != null) rs.close(); } catch (Exception e) { e.printstacktrace(); } } public static voID main(String[] args) { /*try { System.out.println("
测试数据库连接开始\n"); Connection conn=DBConnection.getConn(); String sql1="select * from dbo.Se"; PreparedStatement pre = conn.prepareStatement(sql1); ResultSet rs=pre.executequery(); while(rs.next()){ System.out.println(rs.getString(1)); } System.out.println("测试数据库连接结束"); DBConnection.close(conn,pre,rs); } catch (Exception e) { e.printstacktrace(); }*/ } } 总结
以上是内存溢出为你收集整理的sqlserver 连接测试全部内容,希望文章能够帮你解决sqlserver 连接测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)