java之好友查询

该程序用于好友查询主要思想如下:

****首先用户要能查询到自己所有好友的信息,这些信息需要保存在数据表中

****查询到所有好友之后,由于每个时期都会有好友的离去和新朋友的出现所以要有增加和删除好友的功能

具体实现:

运用java语言连接mysql数据库,使用sql语句和java语句相结合完成对数据表信息的操作

重点:

******con=DriverManager.getConnection(url, user, passwd);//连接数据库

****** ps=con.prepareStatement("insert into haopengyou(name,school,sex)"+"values (?,?,?)");//插入新好友信息

******ps=con.prepareStatement("update haopengyou set name=?,school=?  where name=?");//改变好友信息

******ps=con.prepareStatement("delete from haopengyou where name=?");//删除好友信息

package hjffdjf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.PreparedStatement;

public class MM {
    static String name;
    static String school;
    static String sex;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Connection con = null;//创建Connection对象为连接数据库做准备
         String driver="com.mysql.jdbc.Driver";//加载连接数据库所需要的驱动程序
         String url="jdbc:mysql://localhost:3306/haopengyou";//URL指向所要连接的数据库
         String user="root";//设定连接数据库的用户
         String passwd="root";//设定连接数据库所使用的密码
         try {
            Class.forName(driver);//加载驱动程序
            try {
                con=DriverManager.getConnection(url, user, passwd);//连接数据库
                java.sql.PreparedStatement ps;
                 try {
                    ps=con.prepareStatement("insert into haopengyou(name,school,sex)"+"values (?,?,?)");//向数据库中插入数据
                    ps.setString(1, "zz");//为数据表中的第一个字段设置信息
                    ps.setString(2, "荆楚理工学院");//为数据表中的第二个字段设置信息
                    ps.setString(3, "女");//为数据表中的第三个字段设置信息
                    ps.executeUpdate();//更新数据表
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 ps=con.prepareStatement("update haopengyou set name=?,school=?  where name=?");//更改好友信息
                 ps.setString(1, "mm");//为数据表中的第一个字段设置信息
                 ps.setString(2, "郑州财经学院");//为数据表中的第二个字段设置信息
                 ps.setString(3, "zz");//为数据表中的第三个字段设置信息
                 ps.executeUpdate();//更新数据表
                 ps=con.prepareStatement("delete from haopengyou where name=?");//删除好友信息
                 ps.setString(1, "mm");
                 ps.executeUpdate();//更新数据表
                if(!con.isClosed())//判断数据库是否打开
                {
                    Statement statement=con.createStatement();//创建statement对象执行sql语句
                    String sql="select * from haopengyou";//sql语句
                    ResultSet rs=statement.executeQuery(sql);//ResultSet类用来存放存取的结果
                    while(rs.next())
                    {
                        name=rs.getString("name");
                        school=rs.getString("school");
                        sex=rs.getString("sex");
                        System.out.println(name+"\t"+school+"\t"+sex);
                    }
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

执行效果:

猜你喜欢

转载自blog.csdn.net/llxybm/article/details/81208608