码迷,mamicode.com
首页 > 数据库 > 详细

java-connect-mysql

时间:2017-05-21 13:54:59      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:pass   imp   ddr   static   tsql   cts   div   sel   class   

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();
        }*/
        
    }

}

 

java-connect-mysql

标签:pass   imp   ddr   static   tsql   cts   div   sel   class   

原文地址:http://www.cnblogs.com/tqt--0812/p/6884504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!