java预处理对象_预处理对象,增删改查

package com.orcal.demo001;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;public classDemoLianxi {public static voidmain(String[] args) throws ClassNotFoundException, SQLException {

Class.forName("com.mysql.jdbc.Driver");

String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";

String uname="root";

String ped="123456";//增

/*Connection co=DriverManager.getConnection(url,uname,ped);

String sql="insert into user(uname,pwd) values(?,?)";

PreparedStatement tian=co.prepareStatement(sql);

tian.setString(1, "小红");

tian.setInt(2, 123);

int s=tian.executeUpdate();

System.out.println(s);

tian.close();

co.close();*/

//删

/*Connection co=DriverManager.getConnection(url, uname, ped);

String sql="delete from user where uname=?";

PreparedStatement stat=co.prepareStatement(sql);

stat.setString(1, "小红");

int a=stat.executeUpdate();

System.out.println(a);

stat.close();

co.close();*/

//改

/*Connection co=DriverManager.getConnection(url,uname,ped);

String sql="update user set uname=? where pwd=?";

PreparedStatement sta=co.prepareStatement(sql);

sta.setString(1, "dada");

sta.setInt(2, 999);

int a=sta.executeUpdate();

System.out.println(a);

sta.close();

co.close();*/

//查

Connection co=DriverManager.getConnection(url,uname,ped);

String sql="Select * from user where uname=?";

PreparedStatement sta=co.prepareStatement(sql);

sta.setString(1, "xiaoxiao");

ResultSet aa=sta.executeQuery();while(aa.next()){

String a=aa.getString("uid");

String b=aa.getString("uname");

String c=aa.getString("pwd");

System.out.println(a+b+c);

}

aa.close();

sta.close();

co.close();/*Connection co=DriverManager.getConnection(url,uname,ped);

String sql="select * from user where pwd=?";

PreparedStatement sta=co.prepareStatement(sql);

sta.setInt(1, 111);

ResultSet rs=sta.executeQuery();

while(rs.next()){

String uid=rs.getString("uid");

String uname1=rs.getString("uname");

String pwd=rs.getString("pwd");

System.out.println(uid+uname+pwd);

}

rs.close();

sta.close();

co.close();*/}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值