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员 ...
随机推荐
- 【EatBook】-NO.1.EatBook.1.JavaData.1.001-《JSON 必知必会-Introduction to JavaScript Object Notation》-
1.0.0 Summary Tittle:[EatBook]-NO.1.EatBook.1.JavaData.1.001-<JSON 必知必会-Introduction to JavaScrip ...
- Linux-nmon系统性能监控工具的使用及报表产出
在进行性能测试的时候,需要获取服务器的各项指标,例如 CPU.MEM.I/O.DISK 等.网上有很多的监控工具,nmon 就是其中的一个,其可与 JMeter结合使用,测试系统的性能.其概要的介绍, ...
- java多线程小题一瞥
有如下线程类定义: public class MyThread extends Thread { private static int num = 0; public MyThread() { num ...
- HTTP参数污染【转】
HTTP参数污染注入源于网站对于提交的相同的参数的不同处理方式导致. 例如: www.XX.com/a?key=ab&key=3 如果服务端返回输入key的值,可能会有 一: ab 二:3 三 ...
- MyBatis基础入门《十三》批量新增数据
MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...
- TP文件上传
一.单文件上传 <form action="__ACTION__" enctype="multipart/form-data" method=" ...
- 2. Python3输入与输出
数据的输入和输出操作是计算机最基本的操作,本节只研究基本的输入与输出,基本输入是指从键盘上输入数据的操作,基本输出是指屏幕上显示输出结果的操作. 2.1基本输入和输出 常用的输入与输出设备有很多,如摄 ...
- 18. 4Sum(双指针)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- c++引用和指针的彻底理解
★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指 ...
- 05 enumerate index使用
# enumerate 自动生成一列, 默认0开始,每次自增+1li = ["电脑","鼠标垫","U盘","游艇"]f ...