首页 新闻 会员 周边

jdbc链接数据库

0
[已解决问题] 解决于 2015-07-31 14:51

public class BaseDao {
   public static void main(String args[]) {    
        // Create a variable for the connection string.    
       // String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;"    
         //       + "databaseName=Test;integratedSecurity=true;";         
         String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=newuStandard2012;user=sa;password=newu";    //sa身份连接     ;user=sa;password=newu
    
       // String url2 = "jdbc:sqlserver://127.0.0.1:1433;databaseName=Test;integratedSecurity=true;";//windows集成模式连接    
     
        // Declare the JDBC objects.    
         Connection con = null;    
         Statement stmt = null;    
         ResultSet rs = null;    
    
         try {    
            // Establish the connection.    
            System.out.println("begin.");    
           
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");     //SQLServerDriver
            
             System.out.println("888888");
            
             con = DriverManager.getConnection(url);  
            
             System.out.println("end.");    
     
             // Create and execute an SQL statement that returns some data.    
           String SQL = "SELECT * FROM newu_log_alarm";    
           stmt = con.createStatement();    
           rs = stmt.executeQuery(SQL);    
    
          // Iterate through the data in the result set and display it.    
            while (rs.next()) {    
                 System.out.println(rs.getString(1) + " " + rs.getString(2)+" " + rs.getString(3)+" " + rs.getString(4)+" " + rs.getString(5)+" " + rs.getString(6)+" " + rs.getString(7)+" " + rs.getString(8)+" " + rs.getString(9)+" " + rs.getString(10)+" " + rs.getString(11)+" " + rs.getString(12)+" " + rs.getString(13)+" " + rs.getString(14));    
                }    
         }    
         // Handle any errors that may have occurred.    
         catch (Exception e) {    
            e.printStackTrace();    
         }    
              finally {    
            if (rs != null)    
                 try {    
                    rs.close();    
                 } catch (Exception e) {    
                 }    
             if (stmt != null)    
                 try {    
                     stmt.close();    
                 } catch (Exception e) {                   
                  
                 }    
             if (con != null)    
                 try {    
                     con.close();    
                 } catch (Exception e) {    
                 }           
             }    
     }
}各位我想问问如果我想在main方法之外在加两个方法public ResultSet getResultSet(String sql1, Object[] params, int pageNow)

和public boolean modify(String sql, Object[] params)

那该怎么做了,谢谢各位师哥,师姐了

醉翁之意的主页 醉翁之意 | 初学一级 | 园豆:162
提问于:2015-07-31 11:32
< >
分享
最佳答案
0

写成员方法,把需要的部分抽取出来,你要理解面向对象,这个应该很容易的

奖励园豆:5
悬崖边上 | 菜鸟二级 |园豆:220 | 2015-07-31 12:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册