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员 ...
随机推荐
- OpenCV矩形检测
OpenCV矩形检测 需求:提取图像中的矩形,图像存在污染现象,即矩形区域不是完全规则的矩形. 思路一:轮廓法 OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像, ...
- ORM模板层
1.模板语言之变量 def index(request): name='lqz' age=18 ll=['name','age'] dic={'name':name,'age':age} class ...
- spring——事务管理
1.spring支持编程式事务管理和声明式事务管理. 编程式事务管理:编程式事务管理使用TransactionTemplate或者直接使用最底层的PlatformTransactionManager. ...
- 【LeetCode每天一题】Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- Photoshop去图片水印——适用复杂图片上有水印
该方法适合复杂图片上有水印的,不过这个只适合水印只是文字而没有背景的那种.不是所有的水印图片都适合处理.下面是处理前后的对照 工具/原料 photoshop8.0 方法/步骤 1 打开需要 ...
- Dotfuscator 使用图解教程
Dotfuscator:是.NET混淆器和压缩器,它可以帮助您防止您的应用程序被反编译.同时,它还可以使得您的应用程序更加小巧以及高效.我用的是4.9版本的Dotfuscator,Dotfuscato ...
- 01JAVA语言基础课后作业
1.问题 一个Java类文件中真的只能有一个公有类吗? 请使用Eclipse或javac检测一下以下代码,有错吗? 回答 真的只能有一个公有类 一个Java源文件中最多只能有一个public类,当有 ...
- 提高Linux运维效率的30个命令行常用快捷键
提高Linux运维效率的30个命令行常用快捷键 表4-1 30个常用快捷键 快捷键 功能说明 最有用快捷键 tab 命令或路径等的补全键,Linux最有用快捷键* 移动光标快捷键 Ctrl+a 光标 ...
- Install the mongdb
#!/bin/bash#Function: Install the mongdb#Author: WangDonghui#Date: 20180124 #Installing mongdbecho & ...
- Javascript-全局函数和局部函数作用域的理解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...