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日

最近浏览
