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

jdbc链接mysql

时间:2018-01-25 18:26:58      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:statement   sele   编译   语句   rom   driver   util   cut   state   

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.junit.Test;

public class test {br/>@Test
public void testJdbcInsert(){
PreparedStatement prepareStatement = null;
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获得链接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
//3.编写sql语句
String sql ="insert into product values (null,?,?,?)";
//4.预编译
prepareStatement = connection.prepareStatement(sql);
//5.设置参数
prepareStatement.setString(1, "苹果");
prepareStatement.setDouble(2, 3.5);
prepareStatement.setObject(3, new Date());
//6.执行
prepareStatement.executeUpdate();

    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        //7.关闭资
        try {
            if(prepareStatement!=null){
                prepareStatement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
@Test
public void testJdbcSelect(){
    PreparedStatement prepareStatement = null;
    Connection connection = null;
    ResultSet executeQuery = null;
    try {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得链接
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
        //3.编写sql语句
        String sql ="select * from product where pid=?";
        //4.预编译
        prepareStatement = connection.prepareStatement(sql);
        //5.设置参数
        prepareStatement.setInt(1, 1);
        //6.执行
        executeQuery = prepareStatement.executeQuery();

        while(executeQuery.next()){
            System.out.println(executeQuery.getInt(1));
            System.out.println(executeQuery.getString(2));
            System.out.println(executeQuery.getDouble(3));
            System.out.println(executeQuery.getDate(4));
        }
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }finally {
        //7.关闭资
        try {
            if(executeQuery!=null){
                executeQuery.close();
            }
            if(prepareStatement!=null){
                prepareStatement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

}
/*
注意jdbc的修改 删除 添加都除了sql语句都差不多,所以这里就只写了一个添加

预编译不仅能提高性能还能防止sql注入
*/

jdbc链接mysql

标签:statement   sele   编译   语句   rom   driver   util   cut   state   

原文地址:http://blog.51cto.com/13579086/2065114

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