mysql安装地址

https://dev.mysql.com/downloads/mysql/

mysql安装教程

http://www.runoob.com/mysql/mysql-install.html

my.ini配置文件,放在与bin文件同级位置

[client]
port=3306
default-character-set=utf8
[mysqld] 
# 设置为自己MYSQL的安装目录 
basedir=D:\mysql-5.7.20-winx64
# 设置为MYSQL的数据目录 
datadir=D:\mysql-5.7.20-winx64\data
port=3306
character_set_server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER
#开启查询缓存
explicit_defaults_for_timestamp=true
skip-grant-tables

mysql的初始密码设置

mysql -u root -p '新的密码'

mysql驱动下载

https://dev.mysql.com/downloads/connector/j/下滑点击下载

JDBC-Mysql

2.建立项目,右键选择Build path,选择Configure buile path,

JDBC-Mysql

编写简单代码运行

	/*
	 * 使用jdbc连接数据库
	 */
public class TestJDBC {
	public static void main(String[] args) {
		//1.加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//2.创建连接
		String url = "jdbc:mysql://localhost:3306/jb1";
		String user = "root";
		String password = "root";
		//连接对象
		Connection con = null;
		//命令对象
		//预编译对象
		PreparedStatement ps = null;
		//结果集对象
		ResultSet resultSet = null;
		int stuid = 2;
		try {
			//编写sql语句
			String sql = "SELECT stuId,stuName,stuAge,stuAddress FROM	student WHERE stuid = ?";
			con = DriverManager.getConnection(url, user, password);
			//预编译sql语句
			ps = con.prepareStatement(sql);
			//填充占位符
			ps.setInt(1,stuid);
			//通过预编译对象调用excuteQuery()执行sql语句
			resultSet = ps.executeQuery();
			//判断结果集中有无结果
			if(resultSet.next()) {
				//获取结果  通过getxxx(int columnIndex)列索引从1开始
				//getxxx(String columnLabel)列名
				String userName = resultSet.getString("stuName");
				System.out.println(userName);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			//释放资源
			//原则:最早打开,最晚关闭
			try {
				resultSet.close();
				ps.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

 

相关文章:

  • 2021-09-13
  • 2021-08-22
  • 2022-12-23
  • 2022-01-07
  • 2022-01-07
  • 2022-01-07
  • 2021-12-27
  • 2021-05-31
猜你喜欢
  • 2022-02-01
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2022-02-05
相关资源
相似解决方案