JDBC 连接 SqlServer
三种方式:
1、jdk 1.6 连接sql server 2000, 依赖包:msbase.jar,mssqlserver.jar,msutil.jar
String m_Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String m_Url = "jdbc:microsoft:sqlserver://Ip:Port;DatabaseName=dbName";
2、jdk 1.6 连接sql server 2005 , 依赖包:jtds-1.2.6.jar
String m_Driver = "net.sourceforge.jtds.jdbc.Driver";
String m_Url = "jdbc:jtds:sqlserver://Ip:Port/dbName";
3、jdk 1.6 连接sql server , 依赖包:sqljdbc4.jar
String m_Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String m_Url = "jdbc:sqlserver://Ip:Port;DatabaseName=dbName";
Demo:
例子中采用sqljdbc方式连接:
java -cp .:/home/xxx/sqljdbc4.jar JdbcSqlserver "dbName" "querySql"
import java.sql.DriverManager;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;/** * Created with IntelliJ IDEA. * User: xxx * Date: 13-11-28 * Time: 下午4:25 * To change this template use File | Settings | File Templates. */public class JdbcSqlserver { Connection conn = null; public JdbcSqlserver() { } public void getConnection(String URL, String username, String password) { if (URL==null || username ==null || password == null || URL.isEmpty() || username.isEmpty() || password.isEmpty()){ return; } String m_Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; try { Class.forName(m_Driver).newInstance(); conn = DriverManager.getConnection(URL, username, password); } catch (Exception e) { e.printStackTrace(); } return; } public String[][] executeQuery(String query) { if(conn==null || query ==null || query.isEmpty()) return null; Statement m_Statement = null; ResultSet m_ResultSet = null; int row = 0; int column = 0; try{ m_Statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); m_ResultSet = m_Statement.executeQuery(query); column = m_ResultSet.getMetaData().getColumnCount(); //获取列数; m_ResultSet.last(); row = m_ResultSet.getRow(); //获取行数; } catch (SQLException ex) { ex.printStackTrace(); return null; } String[][] results = new String[row][column]; int i=0; try{ if(m_ResultSet.first())//回到第一行; { for(int j=1;j<=column;j++){ results[0][j-1] = m_ResultSet.getString(j); System.out.print(results[i][j-1].trim()+"/t"); } System.out.print("/n"); i++; } while (m_ResultSet.next()) { for(int j=1;j<=column;j++){ results[i][j-1] = m_ResultSet.getString(j); System.out.print(results[i][j-1].trim()+"/t"); } i++; System.out.print("/n"); } } catch (SQLException ex) { ex.printStackTrace(); } finally { try { if (m_ResultSet != null) m_ResultSet.close(); if (m_Statement != null) m_Statement.close(); } catch (SQLException ex) { ex.printStackTrace(); return results; } } return results; } public void close() { try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { JdbcSqlserver dbTest = new JdbcSqlserver(); dbTest.getConnection("jdbc:sqlserver://Ip:Port;DatabaseName="+args[0],"userName","password"); dbTest.executeQuery(args[1]); dbTest.close(); }}
>更多相关文章
- 10-26Whitewidow SQL漏洞扫描工具演示
- 10-26SQL黑客注入防御与绕过的多种姿势
- 12-23SQLServer数据库操作总结(sql语法的使用)
- 12-21C#连接Sqlite
- 12-21ORACLE数据库学习之SQL性能优化详解
- 12-21解决SQLSERVER2008数据库日志文件占用硬盘空间问题
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 12-05亚马逊推出新一代基础模型 任意模态生成大模
- 12-05OpenAI拓展欧洲业务 将在苏黎世设立办公室
- 12-05微软质疑美国联邦贸易委员会泄露信息 督促其
- 12-05联交所取消宝宝树上市地位 宝宝树:不会对公
- 12-04企业微信致歉:文档打开异常已完成修复
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御