package com.mysql;

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

public class Mysql {

	public static void main(String[] args) {
		Connection conn = null;
		try {
			String userName = "root";
			String password = "111111";
			String jdbcurl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(jdbcurl, userName, password);
			String sql = "select id,name,status from test";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			String result = "";
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int status = rs.getInt("status");
				result += id + "\t" + name + "\t" + status + "\n";
			}
			System.out.println(result);
			pstmt.close();
		} catch (Exception e) {
			System.err.println("Cannot connect to database server,Exception:"
					+ e.getMessage());
		} finally {
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}
	}

}
最近下载更多
yufeng520  LV1 2023年6月14日
婆婆12254  LV2 2023年6月12日
cxdxfx12  LV14 2022年6月8日
3199625134  LV10 2022年5月19日
Zeorwyc  LV8 2022年4月27日
dengjunjun  LV15 2022年3月12日
skystory  LV11 2020年9月25日
wyt1234  LV1 2020年3月22日
三无mareblue  LV3 2019年10月7日
feiying0023  LV8 2019年8月15日
最近浏览更多
安东尼online  LV11 3月8日
cz8857216  LV4 1月5日
hhhhui  LV1 2023年11月12日
2131234536546  LV7 2023年9月8日
穿山甲1001  LV4 2023年6月25日
yufeng520  LV1 2023年6月14日
婆婆12254  LV2 2023年6月12日
胡修洋 2023年6月5日
暂无贡献等级
微信网友_6448337730736128  LV1 2023年4月25日
微信网友_5992582549164032  LV6 2023年3月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友