jdbc --例子7
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class Test { public static void main(String[] args){
Connection conn=null; try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf-8","root","");
System.out.println("连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//
if (null!=conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
}
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; //Statement
public class Test02 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
String name="kitty";
int health=100;
int love =10;
String strain="可爱的小公举3";
//
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet","root","");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//3.
try {
stmt=conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer sbSql=new StringBuffer(
"insert into dog(name,health,love,strain)values('");
sbSql.append(name+"',");
sbSql.append(health+",");
sbSql.append(love+",'");
sbSql.append(strain+"')");
try {
stmt.execute(sbSql.toString());
System.out.println("添加数据成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (null !=stmt) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
if(null!=conn){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
} }
}
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; //更新宠物Statement
public class Test03 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null; //1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.建立连接
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet","root",""); stmt=conn.createStatement();
stmt.executeUpdate("update dog set health=1000,love=1500 where id=1");
System.out.println("成功更新狗狗信息");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
if (null !=stmt) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
if(null!=conn){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
} } }
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; //查询所有宠物Statement和ResultSet
public class Test04 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet","root","");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//3.查询并输入狗狗信息
try {
stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs=stmt.executeQuery("select id,name,health,love,strain from dog");
System.out.println("狗狗信息列表");
System.out.println("编号\t姓名\t健康值\t亲密度\t品种");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt("health")+"\t");
System.out.print(rs.getInt("love")+"\t");
System.out.println(rs.getString("strain"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null!=rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=stmt){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
}
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; //查询并输出宠物主人信息
public class Test05 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet","root","");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//3.
try {
stmt=conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
rs=stmt.executeQuery("select id ,name,money from master");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("主人信息列表");
System.out.println("编号\t姓名\t元宝数");
try {
while(rs.next()){
System.out.print(rs.getInt("id")+"\t");
System.out.print(rs.getString("name")+"\t");
System.out.println(rs.getInt("money")); }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null!=rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=stmt){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner; //
public class Test06 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//根据控制台提示输入用户账号和密码
Scanner input =new Scanner(System.in);
System.out.println("宠物主人登录");
System.out.println("请输入姓名:");
String name =input.next();
System.out.println("请输入密码:");
String password=input.next();
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf-8","root","");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//判断宠物主人登录成功
try {
stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql="select * from master where name='"+name+"' and password='"+password+"'";
System.out.println(sql);
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(rs.next())
System.out.println("登录成功,欢迎你");
else
System.out.println("登录失败,请重新输入!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null!=rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=stmt){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
}
package cn.kitty.o1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException; //preparedStatment 更新多条狗狗信息
public class Test07 {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement pstmt=null;
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.建立连接
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf-8","root","");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//3.更新狗狗数据到数据库
String sql="update dog set health=?,love=? where id=?";
try {
pstmt=conn.prepareStatement(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
pstmt.setInt(1, 90);
pstmt.setInt(2, 90);
pstmt.setInt(3, 1);
pstmt.executeUpdate();
pstmt.setInt(1, 90);
pstmt.setInt(2, 90);
pstmt.setInt(3, 2);
pstmt.executeUpdate();
System.out.println("更新狗狗信息成功");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{ if(null!=pstmt){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }}
jdbc --例子7的更多相关文章
- Spring JDBC 例子
http://www.yiibai.com/spring/spring_jdbc_example.html 要了解有关Spring JDBC框架与JdbcTemplate类的概念,让我们写这将实现所有 ...
- 编写一个简单的jdbc例子程序
package it.cast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...
- jdbc例子
public class ConnMysql { public static void main(String[] args) throws ClassNotFoundException, SQLEx ...
- Mybatis的缺陷
Mybatis是业界非常流行的持久层框架,轻量级.易用,在金融IT领域完全是领军地位,比Hibernate更受欢迎,优势非常多,也是非常值得我们学习的.但Mybatis并不尽善尽美,其自身的设计.编码 ...
- ref:Spring JdbcTemplate+JdbcDaoSupport实例
ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...
- Spring 4 官方文档学习(九)数据访问之事务管理
说明:未整理版,未完待续,请绕行 本部分的重点是数据访问以及数据访问层与业务层之间的交互. 1.Spring框架的事务管理 介绍 http://docs.spring.io/spring/docs/c ...
- Spring JdbcTemplate+JdbcDaoSupport实例
在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...
- jqGrid(struts2+jdbc+jsp)增删改查的例子
前几日一直在找关于Java操作jqgrid返回json的例子,在网上也看了不少东西,结果都没几个合理的,于是本人结合网上的零散数据进行整理,完成了 一个比较完整的jqgrid小例子,考虑到还有很多 ...
- JDBC调用存储过程的例子
下面是我学到了Oracle存储过程,在这里跟大家简单的分享一下利用JDBC调用存储过程的例子: 废话就不啰嗦,现在就直接上机代码. 首先我利用的是Oracle中默认的 scott 数据库里的 emp员 ...
随机推荐
- 静态库lib和动态库dll相关总结
1.静态链接库LIB和动态链接库DLL的区别 若采用静态链接库,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了.而动态动态链接库则不必被包含在最终 EXE 文件中,EXE 文件执行时可 ...
- delete 删除对象属性
删除属性要直接删.
- bootstrap modal与select2使用冲突解决
今天发现项目使用bootstrap modal 与 jquery select2 结合时发现select2不起作用,点击select框不显示选项,查阅资料后发现是因为modal层遮挡了select2的 ...
- cocos2dx - JS - 碰撞检测
碰撞检测是游戏的一个重要组成部分,我们这里使用一种最简单的方法,就是获取精灵的矩形碰撞框.当然圆形的碰撞检测也比较简单,其他形状就复杂多了.首先是如何获取矩形碰撞框:var hBox=this.her ...
- sift拟合详解
1999年由David Lowe首先发表于计算机视觉国际会议(International Conference on Computer Vision,ICCV),2004年再次经David Lowe整 ...
- Tomcat任意文件上传漏洞CVE-2017-12615
文章来源:https://blog.csdn.net/qq1124794084/article/details/78044756 漏洞影响的tomcat版本为tomcat7.0.0-7.0.81版本 ...
- UVAL 4728 Squares(旋转卡壳)
Squares [题目链接]Squares [题目类型]旋转卡壳 &题解: 听着算法名字,感觉挺难,仔细一看之后,发现其实很简单,就是依靠所构成三角行面积来快速的找对踵点,就可以省去很多的复杂 ...
- Mod制作第一个物品
在通过Mod制作第一个物品时需要 一下几个步骤: 创建一个物品 通常在创建这个物品类的时候会继承Item或者是Block父类(因为Mod中的物品都是这个两个类的子类),在给类中使用了this.setU ...
- Yii2 Restful api搜索实现
- fzu2204 dp
2015-10-06 19:31:05 n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个. 每组包含n,表示球的个数.(1 <= ...