首页 > 代码库 > Mysql最后补充+Java连接数据库
Mysql最后补充+Java连接数据库
Truncate table 表名
直接删除表中全部数据,与delete不同的是,此方法无法使用where选择,只能全部删除。
truncate table users;
Java连接数据库:
package com.edu.gkh;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Homework {
static Statement sc=null;
static Scanner sca=new Scanner(System.in);
static String username;
public static void main(String[] args) throws Exception {
getStatement();
String name2="";
String pwd2="";
for(;;){
System.out.println("请输入注册账号");
String name=sca.next();
System.out.println("请输入密码");
String pwd=sca.next();
System.out.println("请再次输入密码");
String pwd1=sca.next();
if(pwd1.equals(pwd)){
name2=name;
pwd2=pwd;
System.out.println("注册成功");
break;
}
else{
System.out.println("两次密码不一致,请重新输入;是否重新注册yes/no");
char regist=sca.next().charAt(0);
if(regist==‘y‘){
System.out.println("请重新输入");
}
else if(regist==‘n‘){
System.out.println("您已经退出注册");
}
else{
System.out.println("您的输入非法");
}
}
}
System.out.println("欢迎来到J18银行");
System.out.println("请登录:");
System.out.println("用户名:");
username=sca.next();
System.out.println("密码:");
String password=sca.next();
int num=queryAccount(username,password);
if(num==1){
System.out.println("登陆成功");
for(;;){
System.out.println("请选择交易类型:");
System.out.println("1、存钱 2、取钱 3、查询余额");
int cz=sca.nextInt();
if(cz==1){
cun();
}else if(cz==2){
qu();
}else if(cz==3){
query();
}else{
System.out.println("谢谢使用!");
break;
}
}
}else{
System.out.println("登录失败");
}
}
public static void cun() throws Exception{
System.out.println("请输入你的存款金额:");
double money=sca.nextDouble();
String sql="update account set money=money+"+money;
boolean a=sc.execute(sql);
if(!a){
System.out.println("存款成功!");
}
}
public static void qu() throws Exception{
System.out.println("请输入你的取款金额:");
double money=sca.nextDouble();
String sql="update account set money=money-"+money;
System.out.println(sql);
boolean a=sc.execute(sql);
if(!a){
System.out.println("取款成功!");
}
}
public static int queryAccount(String username,String password) throws Exception{
String sql="select * from account where aname=‘"+username+"‘and apwd=‘"+password+"‘";
ResultSet rs=sc.executeQuery(sql);
int num=0;
while(rs.next()){
num++;
}
return num;
}
public static void query() throws Exception{
String sql="select money from account where aname=‘"+username+"‘";
ResultSet rs=sc.executeQuery(sql);
double money=0;
while(rs.next()){
money=rs.getDouble(1);
}
System.out.println("你的账户余额:"+money);
}
public static void getStatement() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/atm";
String username="root";
String password="root";
Connection c=DriverManager.getConnection(url,username,password);
sc=c.createStatement();
}
}
Mysql最后补充+Java连接数据库