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();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值