java-connect-mysql

package connectmysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Connectsql {

    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";        //驅動路徑
        String url = "jdbc:mysql://localhost:3306/test";//數據庫地址
        String user = "root";                            //訪問數據庫的用戶名
        String password = "123456";                        //用戶密碼
        
        //數據刪除
        /*try{
            String name = "tqt";
            int i;
            
            Class.forName(driver);                        //加載驅動
            Connection con = DriverManager.getConnection(url, user, password);
            if(!con.isClosed()){
                System.out.println("連接數據庫成功:");
                String sql="delete from user_70 where name=?";
                PreparedStatement query =con.prepareStatement(sql);
                query.setString(1, name);
                i=query.executeUpdate();                //影響的行數
                System.out.println(i);
            }
            
        } catch (Exception e){
            e.printStackTrace();
        }*/
        
        
        //數據修改
        /*try{
            String name = "tqt";
            String address = "sichuan";
            String pwd = "54188";
            int i;
            
            Class.forName(driver);                        //加載驅動
            Connection con = DriverManager.getConnection(url, user, password);
            if(!con.isClosed()){
                System.out.println("連接數據庫成功:");
                String sql="update user_70 set address=?, password=? where name=?";
                PreparedStatement query =con.prepareStatement(sql);
                query.setString(1, address);
                query.setString(2, pwd);
                query.setString(3, name);
                i=query.executeUpdate();                //影響的行數
                System.out.println(i);
            }
            
        } catch (Exception e){
            e.printStackTrace();
        }*/
        
        
        //數據插入
        /*try{
            String name = "tqt";
            String address = "mianyang";
            String pwd = "081200";
            int i;
            
            Class.forName(driver);                        //加載驅動
            Connection con = DriverManager.getConnection(url, user, password);
            if(!con.isClosed()){
                System.out.println("連接數據庫成功:");
                String sql="insert into user_70(name, address, password)values(?, ?, ?)";
                PreparedStatement query =con.prepareStatement(sql);
                query.setString(1, name);
                query.setString(2, address);
                query.setString(3, pwd);
                i=query.executeUpdate();                //影響的行數
                System.out.println(i);
            }
            
        } catch (Exception e){
            e.printStackTrace();
        }*/
        
        
        //數據查詢
        /*try{
            Class.forName(driver);                        //加載驅動
            Connection con = DriverManager.getConnection(url, user, password);
            if(!con.isClosed()){
                System.out.println("連接數據庫成功:");
                Statement st = con.createStatement();    //建立Statement對象
                String sql="select * from user_70";
                ResultSet rs = st.executeQuery(sql);    //查詢以後返回結果集
                while(rs.next()){
                    System.out.println(rs.getString("name")+"\t"+rs.getString("address")+"\t"+rs.getString("password"));
                }
                rs.close();
            }
            con.close();
            
        } catch (Exception e){
            e.printStackTrace();
        }*/
        
    }

}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息