连接数据库

来源:互联网 发布:哪里买古典音乐cd知乎 编辑:程序博客网 时间:2024/05/16 17:44
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Jdbc;


import Model.LoginInf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;


/**
 
 * @author HP-Developerver
 */
public class Dao {
    protected static String dbClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    protected static String dbUr1="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
    protected static String dbUser="sa";
    protected static String dbPwd="1234";
    protected static String second=null;
    private static Connection conn=null;
    private Dao(){
           try { 
                if (conn==null) {
                Class.forName(dbClassName);
                conn= DriverManager.getConnection(dbUr1, dbUser,dbPwd);
                }
                else 
                    return;
            }
           catch (Exception ee){
               ee.printStackTrace();
           }
    }
    
  static ResultSet executeQuery(String sql){
        try{
            
            if(conn==null)
                new Dao();
                return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
            
            }catch(SQLException e){
                    e.printStackTrace();
                    return null;
                    }finally{
                            }
    }
    
    public static int executeUpdate(String sql){
        try{
            System.out.println(sql);
        if(conn==null)
            new Dao();
        return conn.createStatement().executeUpdate(sql);
        }catch (SQLException e){
        System.out.println(e.getMessage());
        return -1;
        }
        
    }
    


}
0 0