JDBC MySQL连接

环境InterlliJ2016.3  MySQL5.7.12

gradle依赖库:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    compile 'org.slf4j:slf4j-log4j12:1.7.21'

    compile 'mysql:mysql-connector-java:5.1.39'
}

DBService.java

package com.mind.core.db.impl;

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; /** * 数据库服务 * Created by Lovell on 16/6/18. */ public class DBService { private static Logger logger = LoggerFactory.getLogger(DBService.class); private static final String DB_CONFIG_FILE = "/db.properties"; // 数据库服务器addr private String db_addr = null; // 数据库名称 private String db_name = null; // 数据库连接数 private short db_max_conn; // 数据库登录用户名 private String db_username = null; // 数据库登录密码 private String db_password = null; // 数据库连接 private Connection connection; private static DBService dBService; public static DBService getInstance(){ if (dBService == null) { dBService = new DBService(); } return dBService; } public void start() throws IOException { Properties properties = new Properties(); InputStream in = DBService.class.getClass().getResourceAsStream(DB_CONFIG_FILE); properties.load(in); db_username = String.valueOf(properties.getProperty("db_username")); db_password = String.valueOf(properties.getProperty("db_password")); String addr = String.valueOf(properties.getProperty("db_addr")); String db_name = String.valueOf(properties.getProperty("db_name")); if (addr == null || addr.length() == 0) { logger.error("配置的数据库ip地址错误!"); System.exit(0); } String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://"+ addr +"/" + db_name + "?characterEncoding=utf8&useSSL=false"; try { // 加载驱动程序 Class.forName(driver); // 连接数据库 connection = DriverManager.getConnection(url, db_username, db_password); if (!connection.isClosed()) System.out.println("Succeeded connecting to the Database!"); } catch(ClassNotFoundException e) { System.out.println("Sorry, can't find the Driver!"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } public Connection getConnection() { return connection; } public boolean stop() throws SQLException { if (connection != null) { connection.close(); } return true; } }

MySQLTest.java

package com.mind.core.db.impl;

import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by Lovell on 16/6/21.
 */
public class MySQLTest {
    public static void main(String[] args) throws Exception {
        DBService.getInstance().start();

        // statement用来执行SQL语句
        Statement statement = DBService.getInstance().getConnection().createStatement();

        // 要执行的SQL语句id和content是表review中的项。
        String sql = "select * from login where name='Lovell' and password='123456'";

        // 得到结果
        ResultSet rs = statement.executeQuery(sql);

        if(rs.next()){
            System.out.println("Logon");

        }else{
            System.out.println("Login Faild");
        }
        rs.close();

    }
}

你可能感兴趣的:(JDBC MySQL连接)