1.java中jdbc怎么链接sqlserver数据库??
2.有几种连接sqlserver数据库方法
3.有没有链接sqlserver的thin方法
4.能不能写一下代码

解决方案 »

  1.   

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;/**
     * 驱动程序加载以及建立数据联接工具类
     * @author xiaohai
     */
    public class DataConnection { /**
     * 驱动程序加载以及建立数据联接
     * @return Connection 对象
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException{
    // 加载驱动程序
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    // 连接数据库字符串
    String url = "jdbc:microsoft:sqlserver://localhost:1433(端口);DatabaseName=数据库名称";
    // 返回数据库连接
    return DriverManager.getConnection(url, "数据库用户名", "数据库密码");
    }

    }
      

  2.   

    public class DataBase {
    public Connection conn;
    public Statement stmt;
    public ResultSet rs=null;
    public String sqlstr="";

    public DataBase(){
    this.connect();
    }
    public boolean connect(){
    try{
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    String   url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=stockpile_ms";
    String   user="sa";          
    String   password="123456";          
    Connection   conn=   DriverManager.getConnection(url,user,password); 
    stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
       }catch(Exception e){
       System.out.println("Connect Sql server error:"+e.getMessage());
       return false;
       }
       return true;
    }
    public static void main(String[] args){
    try{
    DataBase db=new DataBase();
    db.connect();
       }catch(Exception e){
       e.printStackTrace();
       }
    }
    }
    public boolean query() throws Exception{
    try{
    DataBase db=new DataBase();  
    String cur_Invoiceid = this.getInvoice_id();
    String cur_Supplier_company = this.getSupplier_company();
    String cur_purchase_person = this.getPurchase_person();
    db.connect();
    String sql= "Select * From Storage_inventories  where Storage_inventories.invoice_isregistrated=1 and Storage_inventories.credentials_num like '%"+cur_Invoiceid+"%'"
    +" and Storage_inventories.supplier_company like '%"+cur_Supplier_company+"%' and Storage_inventories.purchase_person like'%"+cur_purchase_person+"%'";
    System.out.println(sql);
    rs=db.stmt.executeQuery(sql);
    }catch(Exception e){
       System.out.println("Connect Sql server error:"+e.getMessage());
       return false;
    }
    int cur_pagecount = 0;
    int cur_pagesize = 100;
    int cur_recordcount = 0;
    list =new Vector();//申请空间,以向量形式存储每一条记录。
    while(rs.next()){
    Storage_Inventories storage_inventories = new Storage_Inventories();
    storage_inventories.setPurchasePlan_num(rs.getString("purchasePlan_num"));
    storage_inventories.setType_num1(rs.getString("type_num1"));
    storage_inventories.setType_num2(rs.getString("type_num2"));
    storage_inventories.setType_num3(rs.getString("type_num3"));
    storage_inventories.setMaterial_num(rs.getString("material_num"));
    storage_inventories.setMaterial_name(rs.getString("material_name"));
    storage_inventories.setSize(rs.getString("size"));
    storage_inventories.setUnit(rs.getString("unit"));
    storage_inventories.setQuantity(rs.getFloat("quantity"));
    storage_inventories.setApproval_quantity(rs.getFloat("approval_quantity"));
    storage_inventories.setUnitprice(rs.getFloat("unitprice"));
    storage_inventories.setReal_unitprice(rs.getDouble("real_unitprice"));
    storage_inventories.setSupplier_company(rs.getString("supplier_company"));
    storage_inventories.setDate(rs.getString("date"));
    storage_inventories.setPurchase_person(rs.getString("purchase_person"));
    storage_inventories.setPlan_person_approval(rs.getBoolean("plan_person_approval"));
    storage_inventories.setBoss_approval(rs.getBoolean("boss_approval"));
    storage_inventories.setInvoice_isregistrated(rs.getBoolean("invoice_isregistrated"));
    storage_inventories.setIs_checkout(rs.getBoolean("is_checkout"));
    storage_inventories.setCredentials_num(rs.getString("credentials_num"));
    cur_recordcount++;
    if(cur_recordcount==cur_pagesize){
    cur_pagecount++;
    cur_recordcount=0;
    }
    list.addElement(storage_inventories);
    }
    if(cur_recordcount!=0)cur_pagecount++;
    cur_recordcount+=(cur_pagecount-1)*cur_pagesize;
    this.setPageCount(cur_pagecount);
    this.setPageSize(cur_pagesize);
    this.setRecordCount(cur_recordcount);
    this.setPageId(1);
    return true;
    }