MySQL-JDBC

  • 需要导入jar包: mysql-connector-java-5.1.42-bin.jar
  • JDBC
package work;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class database {
     public  static void fun() throws ClassNotFoundException, SQLException{
            System.out.println("****************");
                String driver="com.mysql.jdbc.Driver";
                String url="jdbc:mysql://localhost:3306/javawork";
                String user="sa";
                String password="123456";
                
                Class.forName(driver);
                 Connection conn=DriverManager.getConnection(url, user, password);
                System.out.println(conn);
                Statement str=conn.createStatement();
                String sql="insert into user values('zhangsi','123',' 123@qq.com ');";
                str.executeUpdate(sql);
                 str.close();
                conn.close();
            
      }
        public static void main(String[] args) throws ClassNotFoundException, SQLException
        {
           fun();
        }
}
  • JDBCUNIT
package work;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcUnit {
    
    /**
     * 获得数据库连接
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection getConnection() throws SQLException, ClassNotFoundException{
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/javawork","zm","123456");
    }
    
    /**
     * 关闭数据库连接
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void close(ResultSet resultSet,Statement statement ,Connection connection){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                resultSet=null;
            }
        }
        
        if(statement!=null){
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                statement=null;
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                connection=null;
            }
        }
        
    }
    
}

  • JDBCFUN
package work;
//import java.awt.List;
import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.ResultSet;
//import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class jdbcfun {
//    
    
//    public static void main(String[] args) {
//        userbean users=new userbean();
//        users.setName("123");
//        users.setPassword("2");
//        System.out.println(ispassword(users));
//        
//    }
//
    
    public static boolean finduser(String username){
        
        Boolean bool=false;
        PreparedStatement ps=null;
        Connection conn=null;
        Statement str=null;
        ResultSet re=null;
        try{        
         conn= jdbcUnit.getConnection();        
         str=conn.createStatement();
         String sql="select * from user where name=?;";
         ps=(PreparedStatement) conn.prepareStatement(sql);
         ps.setString(1, username);
         re=ps.executeQuery();
         System.out.println(username);
         if(!re.next())
         {
            
             bool=false;
         }
         else
         {
             bool=true;
         }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            jdbcUnit.close(re, str, conn);
        }
        
        return bool;
    }
    public static void insert(userbean users) {
        Connection conn=null;
        Statement str=null;
        ResultSet re=null;
        PreparedStatement ps=null;
        
        
        try{
             conn= jdbcUnit.getConnection();
        
         str=conn.createStatement();
         String sql="insert into user (name,password,mail)values(?,?,?);";
         ps=(PreparedStatement) conn.prepareStatement(sql);
         ps.setString(1, users.getName());
         ps.setString(2, users.getPassword());
         ps.setString(3, users.getEmail());
        
         ps.executeUpdate();
         ps.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally{
            jdbcUnit.close(re, str, conn);
        }
        
    }
     
    public static boolean ispassword(userbean users){
        Boolean bool=false;
        PreparedStatement ps=null;
        Connection conn=null;
        Statement str=null;
        ResultSet re=null;
        try{        
         conn= jdbcUnit.getConnection();        
         str=conn.createStatement();
         String sql="select password from user where name=?;";
         ps=(PreparedStatement) conn.prepareStatement(sql);
         ps.setString(1, users.getName());
         System.out.println( users.getName());
         re=ps.executeQuery();
        
         if(!re.next())
         {
             bool=false;
         }
         else
         {
             re=ps.executeQuery();
             while(re.next()){
                 String password1=re.getString("password");
                 String password2=users.getPassword();
                 if(password1.equals(password2)){
                    
                     bool=true;
                 }
                 else
                 {
                     bool=false;
                 }
             }
            
         }
        
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            jdbcUnit.close(re, str, conn);
        }
        
        return bool;
    }
    
    
    public static void update(userbean users)
    {
        Connection conn=null;
        Statement str=null;
        ResultSet re=null;
        PreparedStatement ps=null;
        
        try{
             conn= jdbcUnit.getConnection();
        
         str=conn.createStatement();
         String sql="update user set password=? where name=?;";
         ps=(PreparedStatement) conn.prepareStatement(sql);
         ps.setString(2, users.getName());
         ps.setString(1, users.getPassword());
         ps.executeUpdate();
         ps.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally{
            jdbcUnit.close(re, str, conn);
        }
        
    }
    public static void findall(){
        //List list=null;
        Connection conn=null;
        Statement str=null;
        ResultSet re=null;
        try{
            
         conn= jdbcUnit.getConnection();
         str=conn.createStatement();
         String sql="insert into user values(users.name,users.password,users.email);";
         re=str.executeQuery(sql);
          
         while (re.next()) {   
             System.out.println(re.getString("name"));
                System.out.println(re.getString("password"));
                System.out.println(re.getString("mail"));
          }   
            
         
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            jdbcUnit.close(re, str, conn);
        }
        
        
        //return list;
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值