java对mysql的操作

package Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import entity.UserInfo;

public class JDBC {
  Connection conn;
  PreparedStatement ps;
  ResultSet rs;
  /**
   * 写一个连接数据库的方法
   */
  public Connection getConnection(){
    String url="jdbc:mysql://localhost:端口/数据库";
    String userName="root";
    String password="密码";
    try {
      Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      System.out.println("找不到驱动!");
      e.printStackTrace();
    }
    try {
      conn=DriverManager.getConnection(url, userName, password);
      if(conn!=null){
        System.out.println("connection successful");
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
        System.out.println( "connection fail");
      e.printStackTrace();
    }
    return conn;
  }
  /**
   * 写一个查询数据库语句的方法
   */
  public void QuerySql(){
    //1、执行静态SQL语句。通常通过Statement实例实现。   
    // 2、执行动态SQL语句。通常通过PreparedStatement实例实现。   
    // 3、执行数据库存储过程。通常通过CallableStatement实例实现。
    System.out.println("query");
    UserInfo u;
//		j.Connection();
    String sql="select * from userInfo";
    try {
      conn=getConnection();//连接数据库
      ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
      rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句
      // 4.处理结果集
      while(rs.next()){
        u=new UserInfo();
        u.setUserId(rs.getInt("userId"));
        u.setUserName(rs.getString("userName"));
        u.setPassword(rs.getString("password"));
        u.setRemark(rs.getString("remark"));
        System.out.println("uesrName"+u.getUserName());
      }
      System.out.println(rs.next());
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      //释放资源
      try {
        rs.close();
        ps.close();
        conn.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
  @SuppressWarnings("null")
  public int add(UserInfo user){
    UserInfo u =new UserInfo();
    int row=0;
//		j.Connection();
    String sql="insert into userInfo(userName,password) values(?,?)";
    try {
      conn=getConnection();//连接数据库
      ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
//			rs=ps.executeQuery();  // 3.ִ执行SQL语句,緊緊用于查找語句
      //sql語句中寫了幾個字段,下面就必須要有幾個字段
       ps.setString(1, user.getUserName());
      ps.setString(2, user.getPassword());
      // 4.处理结果集
      row=ps.executeUpdate();
      System.out.println(row+user.getUserName()+user.getPassword());
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      try {
        ps.close();
        conn.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      
    }
    return row;
  }
  /**
   * @return修改数据
   */
  public int update(UserInfo user){
    UserInfo u;
    int row=0;
//		j.Connection();
    String sql="update userInfo set userName=?,password=? where userId=?";
    try {
      conn=getConnection();//连接数据库
      ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
//			rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句,緊緊用于查找語句
      //sql語句中寫了幾個字段,下面就必須要有幾個字段
      ps.setString(1, user.getUserName());
      ps.setString(2, user.getPassword());
      ps.setInt(3, user.getUserId());
      // 4.处理结果集
      row=ps.executeUpdate();
      System.out.println(row);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      //释放资源
      try {
//				rs.close();
        ps.close();
        conn.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return row;
  }
  /**
   * @return删除操作
   */
  public int delete(UserInfo user){
    UserInfo u;
    int row=0;
//		j.Connection();
    String sql="delete from userInfo where userId=?";
    try {
      conn=getConnection();//连接数据库
      ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
//			rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句,緊緊用于查找語句
      //sql語句中寫了幾個字段,下面就必須要有幾個字段
      ps.setInt(1, user.getUserId());
      // 4.处理结果集
      row=ps.executeUpdate();
      System.out.println(row);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      //释放资源【執行完sql要記得釋放資源】
      try {
//				rs.close();
        ps.close();
        conn.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return row;
  }
  public static void main(String[] args) {
    JDBC j=new JDBC();
//		j.getConnection();
    j.QuerySql();//在控制台顯示出查找方法
//		UserInfo u=new UserInfo();
//		u.setUserId(5);
//		u.setUserName("cool");
//		u.setPassword("123abc");
//		j.update(u);在控制台顯示出修改方法
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值