标签:mysq test 系统 closed ems mdi cte stat cut
import java.sql.*; // 全部导入
public class DemoFrame{
public static Connection conn = null;
public static Statement stmt = null;
// 尽管将stmt设为全局比较方便,但一个stmt使用多次可能会出现错误(rs未关闭),此时应该先写rs.last();
// 更好的方法,是将stmt 设为局部变量,每次需要执行新语句都重新创建stmt
// Statement stmt1 = conn.createStatement();
// ResultSet rs = stmt1.executeQuery(...);
public static void main(String[] args){
try{
// 连接数据库
conn = DriverManager.getConnection("jdbc:mysql://localhost/myshop?serverTimezone=GMT%2B8&characterEncoding=utf8","root", "");
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DemoFrame frame = new Hahah(); // 创建窗口
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}catch(SQLException ex){
// 输出提示信息
JOptionPane.showConfirmDialog(null,"发生意外错误","系统消息",JOptionPane.CLOSED_OPTION);
ex.printStackTrace();
}
}
};
stmt.executeQuery("...");
/*
MySQL语法格式:
select * from [table_name] where [boolean expression]
e.g: 查询商品名为‘小汽车‘的商品信息
MySQL: select * from items where 名称=‘小汽车‘
Java:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from items where 名称=‘小汽车‘");
*/
stmt.executeUpdate("...");
/*
MySQL语法格式:
insert into [table_name] values(a,b,c,d,e,...)
e.g: 插入一条商品信息
MySQL: insert into items values(‘001‘,‘小汽车‘,100,100,0);
Java:
Statement stmt = conn.createStatement();
stmt.executeUpdae("insert into items values(‘001‘,‘小汽车‘,100,100,0)");
*/
stmt.executeUpdate("...")
/*
MySQL语法格式:
update [table_name] set [c1]=[v1],[c2=v2],... where [boolean expression]
e.g: 修改商品编号为001的商品,使其价格变为100,库存变为200
MySQL: update items set 单价=100 where 编号=‘001‘
Java:
Statement stmt = conn.createStatement();
stmt.executeUpdate("update items set 单价=10,库存=200 where 编号=‘001‘");
*/
这一部分很简单,不需要了解太多
ResultSet rs = stmt.executeQuery("..."); // 获取方式
while(rs.next()){
// rs.getString( cNum )
// rs.getInt( cNum )
}
只有一点需要注意,rs在调用get()方法之前,一定要执行.next()
标签:mysq test 系统 closed ems mdi cte stat cut
原文地址:https://www.cnblogs.com/popodynasty/p/14141086.html