JDBC连接MYSQL

package org.spring.springboot.controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCTest {
public static void main(String[] args) throws Exception {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;

    try {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/mytest";
        String user = "root";
        String password = "123456";
        connection = DriverManager.getConnection(url, user, password);
        // 获取statement,preparedStatement
        String sql = "select * from users where id=?";
        prepareStatement = connection.prepareStatement(sql);
        // 设置参数
        prepareStatement.setLong(1, 1l);
        // 执行查询
        rs = prepareStatement.executeQuery();
        // 处理结果集
        while (rs.next()) {
            System.out.println(rs.getString("userName"));
            System.out.println(rs.getString("name"));
            System.out.println(rs.getInt("age"));
            System.out.println(rs.getDate("birthday"));
        }
    } finally {
        // 关闭连接,释放资源
        if (rs != null) {
            rs.close();
        }
        if (prepareStatement != null) {
            prepareStatement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

}

相关文章

连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软...
JDBCRequest 使用VariableNamesmysql:数据库连接池对象var...
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术...
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java....
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务...
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:...