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();*/}
}