JDBC 连接 SqlServer

浏览:
字体:
发布时间:2013-12-09 23:23:48
来源:

三种方式:

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();    }}
>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();