Java mysql select(Java mysql 查询)

语法说明

select * from 表名
select 字段1,字段2 from 表名

例子如下

package com.pangugle.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBC {

	public static Connection getConnect() throws ClassNotFoundException, SQLException
	{
		// 通过的反射的方式,导入数据库连接驱动
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://192.168.3.201:3306/testpgdb??useUnicode=true&characterEncoding=utf8&jdbcCompliantTruncation=false&useSSL=false&tinyInt1isBit=false";
		Connection conn = DriverManager.getConnection(url, "root", "root");
		System.out.println("connect mysql is success ? " + !conn.isClosed());
		return conn;
	}

	public static void testInsert(String username, String password) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "insert into pangugle_user(user_name, user_password) values(?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);

			int rs = ps.executeUpdate();
			System.out.println("insert 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}



	public static void testDelete(String username) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "delete from pangugle_user where user_name = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);

			int rs = ps.executeUpdate();
			System.out.println("delete 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void testDeleteAll() throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "delete from pangugle_user";
			ps = conn.prepareStatement(sql);

			int rs = ps.executeUpdate();
			System.out.println("delete 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void testUpdate(String username, String password) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "update pangugle_user set user_password = ? where user_name = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, password);
			ps.setString(2, username);

			int rs = ps.executeUpdate();
			System.out.println("update 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void testSelect() throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select * from pangugle_user";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery(sql);
			while(rs.next())
			{
				long userid = rs.getLong("user_id");
				String username = rs.getString("user_name");
				String password = rs.getString("user_password");
				System.out.println(" userid = " + userid + ",  username = " + username + ", password = " + password);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
			rs.close();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException
	{
		// getConnect();

		//testInsert("u1", "p1");
		//testInsert("u2", "p2");
		//testInsert("u3", "p3");

		//testDelete("u1");

		//testUpdate("u2", "p2222");
		testSelect();
	}
}

运算结果如下

connect mysql is success ? true
userid = 3,  username = u2, password = p2222
userid = 4,  username = u3, password = p3