准备工作
1.创建一个java项目导入mysql驱动包
2.在src目录中创建一个新的Java类
JDBC查询:
package com.ATedu.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class jdbctest { public static void main(String[] args) { //链接对象 Connection con=null; //查询对象 PreparedStatement ps=null; //结果集对象 ResultSet rs=null; try { //加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //获取连接 String url="jdbc:mysql://localhost:3306/ssm_mybatis?useSSL=false"; String user="root"; String password="a123456"; con=DriverManager.getConnection(url, user, password); //查询语句 String sql="select * from user where u_sex = ?"; //获取statement ps=con.prepareStatement(sql); ps.setString(1, "0"); //查询输出结果 rs=ps.executeQuery(); while(rs.next()) { System.out.println(rs.getString("u_id")+" "+rs.getString("u_username")+" "+rs.getString("u_sex")); } } catch (Exception e) { e.printStackTrace(); }finally { try { rs.close(); ps.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }