//package db;
//package db;
import java.io.*;
import java.sql.*;
import java.util.*; public  class Class1
{
public static void main(String[] args) throws SQLException,ClassNotFoundException 
{
new DbConnection().openConnection();
}
}
class DbConnection {
Connection conn;
Statement stmt;
ResultSet rset;
private String password;
private String url;
private String user ;
private String jdbc;public DbConnection()throws SQLException,ClassNotFoundException {}public boolean openConnection() throws SQLException{
 
InputStream is = getClass().getResourceAsStream("LocalServer");//取自配置文件
 Properties prop = new Properties();
try {
prop.load(is);
System.out.println("prop");
}
catch (IOException ex) {}
if (is != null) {
try {
is.close();
}
catch (IOException ex1) {
}}
jdbc ="com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
//url = prop.getProperty("url");
//user= prop.getProperty("user");
//password = prop.getProperty("password");
try {
Class.forName(jdbc);
}
catch (ClassNotFoundException ex2) {
ex2.printStackTrace();
}
url="jdbc:microsoft:sqlserver://192.0.1.176:1433;DatabaseName=news";
conn = DriverManager.getConnection(url, "YANHAO", "");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
return true;
}public ResultSet exceuteQuery(String query) throws SQLException{
return stmt.executeQuery(query);
}public void executeUpdate(String query) throws SQLException{
stmt.executeUpdate(query);
}public void close(){
try {
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
if (rset != null) {
rset.close();
}
}
catch (SQLException ex) {
}
}public void finalize(){
this.close();}
public void closeRst(){
stmt =null;
rset=null;
}}

解决方案 »

  1.   

    //文件名为 Class1.java//package db;
    import java.io.*;
    import java.sql.*;
    import java.util.*; public  class Class1
    {
    public static void main(String[] args) throws SQLException,ClassNotFoundException 
    {
    new DbConnection().openConnection();
    }
    }
    class DbConnection {
    Connection conn;
    Statement stmt;
    ResultSet rset;
    private String password;
    private String url;
    private String user ;
    private String jdbc;public DbConnection()throws SQLException,ClassNotFoundException {}public boolean openConnection() throws SQLException{
     
    InputStream is = getClass().getResourceAsStream("LocalServer");//取自配置文件
     Properties prop = new Properties();
    try {
    prop.load(is);
    System.out.println("prop");
    }
    catch (IOException ex) {}
    if (is != null) {
    try {
    is.close();
    }
    catch (IOException ex1) {
    }}
    jdbc ="com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
    //url = prop.getProperty("url");
    //user= prop.getProperty("user");
    //password = prop.getProperty("password");
    try {
    Class.forName(jdbc);
    }
    catch (ClassNotFoundException ex2) {
    ex2.printStackTrace();
    }
    url="jdbc:microsoft:sqlserver://192.0.1.176:1433;DatabaseName=news";
    conn = DriverManager.getConnection(url, "YANHAO", "");
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    return true;
    }public ResultSet exceuteQuery(String query) throws SQLException{
    return stmt.executeQuery(query);
    }public void executeUpdate(String query) throws SQLException{
    stmt.executeUpdate(query);
    }public void close(){
    try {
    if (conn != null) {
    conn.close();
    }
    if (stmt != null) {
    stmt.close();
    }
    if (rset != null) {
    rset.close();
    }
    }
    catch (SQLException ex) {
    }
    }public void finalize(){
    this.close();}
    public void closeRst(){
    stmt =null;
    rset=null;
    }}
      

  2.   

    这样:java -classpath .; DbConnection