`

javaDB

    博客分类:
  • j2ee
阅读更多



import
java.sql.*;

public class DB {

    Connection con;

    Statement
sta;

    ResultSet rs;

    public DB(){

        try {

           
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

        }
catch (ClassNotFoundException ex) {

           
ex.printStackTrace();

        }

    }

    public Connection
getConnection(){

        try {

            con =
DriverManager.getConnection(

                   
"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=petHospital;",

                   
"sa", "");

        } catch (SQLException ex) {

           
ex.printStackTrace();

        }

        return con;

    }

    public
Statement getStatement(){

    try {

        sta =
getConnection().createStatement();

    } catch (SQLException ex) {

       
ex.printStackTrace();

    }

       return sta;

    }

      public
int doInsert(String sql) throws SQLException {

            int
count;

            count = getStatement().executeUpdate(sql);

           
return count;

    }

    public ResultSet getResultSet(String
sql){

        try {

            rs =
getStatement().executeQuery(sql);

        } catch (SQLException ex)
{

            ex.printStackTrace();

        }

        return rs;

   
}

    public void close(){

        try {

            if (sta != null)
{

                sta.close();

            }

            if (con !=
null) {

                con.close();

            }

        } catch
(SQLException ex) {

            ex.printStackTrace();

        }

   
}

}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics