java与数据库的链接


此方法需要知道数据库的Mysql的建立表

还有SQL语言的增删查找



import java.sql.*;
import java.util.Scanner;


import com.mysql.jdbc.Connection;
public class lianjie {
/*
 * 建立数据库的连接;
 */
public static Connection zai() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/shop";
String username="root";
String password="root";
Connection con=(Connection) DriverManager.getConnection(url, username, password);
Statement sta=con.createStatement();
if(sta==null)
{
System.out.println("连接失败");
System.exit(0);
return null;
}
else
{
return con;
}

}
public void add(Connection con) throws ClassNotFoundException, SQLException{
int id;
String usename;
String password;
Statement sta=con.createStatement();
System.out.println("请输入id");
Scanner sc=new Scanner(System.in);
id=sc.nextInt();
System.out.println("请输入姓名");
usename=sc.next();
System.out.println("密码是");
password=sc.next();
String sql="insert into users values(?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
Statement stmt=con.createStatement();
ps.setLong(1,id);
ps.setString(2,usename);
ps.setString(3,password);

  if(ps.executeUpdate()!=0)
 {
System.out.println("添加成功!");
 }
  else
  {
  System.out.println("添加失败!");
  }
  stmt.close();
  ps.close();
  con.close();
}
public void qian(Connection con) throws SQLException{
System.out.println("请输入编号");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
String sql="delete from users where id='"+s+"'";
System.out.println(sql);
Statement stmt=con.createStatement();
if(stmt.executeUpdate(sql)!=0)
 {
System.out.println("删除成功!");
 }
  else
  {
  System.out.println("删除失败!");
  }
stmt.close();
con.close();
}
public void cha(Connection con) throws SQLException{
System.out.println("请输入查找的编号");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from users");
while(rs.next()){
if(s==rs.getInt(1)){
System.out.println("找到!!");
System.out.println(s+" "+rs.getString(2)+"  "+rs.getString(3));
}
}
rs.close();
stmt.close();
con.close();
}
public void denglu(Connection con){
String usename;
String password;
Statement sta=con.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("请输入姓名");
usename=sc.next();
System.out.println("密码是");
password=sc.next();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from users");
while(rs.next()){
if(usename==rs.getString(2)&&password==rs.getString(3))
{
System.out.println("登录成功!!");
}
else{
System.out.println("登录失败!!");
}
}
sta.close();
con.close();
}
public static void main(String args[]) throws ClassNotFoundException, SQLException{
int i;
lianjie pp=new lianjie();System.out.println("1 添加 2 删除 3查找  4登录");
System.out.println("选择");
Scanner sc=new Scanner(System.in);
i=sc.nextInt();
if(i==1)
{
pp.add(pp.zai());
}
else if(i==2)

pp.qian(pp.zai());
}
else if(i==3)
{
pp.cha(pp.zai()); 
}
else
{
pp.denglu(pp.zai());
}


}


}

不足出是,没有更好的应用java的特点,面向对象,而是还是用的学习c的方法,需要改进

应该少用传递数值,尽量用面向对象的方法去实现,由于时间关系,以后会改进的,我理解的面向对象是,少用传递数值,而是把他们看大 话,

这样就能更好的学用面向对象了,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值