⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbconnection.java

📁 基于MVC体系架构编写的login功能模块
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Com;import java.sql.*;/** * * @author XuLiYuan~11 */public class JDBConnection {//    private final String dbDriver =//            "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接sql数据库的方法//    private final String url =//            "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myuddi";    private final String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";     private final String url = "jdbc:sqlserver://localhost:1433; DatabaseName=myuddi";     private final String dbusername = "sa";    private final String dbpassword = "xuliyuan";    private Connection con = null;    public JDBConnection() {        try {            Class.forName(dbDriver).newInstance(); //加载数据库驱动        }         catch (Exception ex) {            System.out.println("数据库加载失败");        }    }//创建数据库连接    public boolean creatConnection() {        try {            con = DriverManager.getConnection(url , dbusername , dbpassword);            con.setAutoCommit(true);        } catch (SQLException e) {            System.out.println(e.getMessage());            System.out.println("creatConnectionError!");        }        return true;    }//对数据库的查询操作    public ResultSet executeQuery(String sql) {        ResultSet rest = null;        try {            if (con == null) {                creatConnection();            }            Statement stmt = con.createStatement();            try {                rest = stmt.executeQuery(sql);            } catch (SQLException e) {                System.out.println(e.getMessage());                return null;            }        } catch (SQLException e) {            System.out.println(e.getMessage());            System.out.println("executeQueryError!");            return null;        }        return rest;    }//关闭数据库的操作    public void closeConnection() {        if (con != null) {            try {                con.close();            } catch (SQLException e) {                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.                System.out.println("Failed to close connection!");            } finally {                con = null;            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -