jdbc連結mysql

來源:互聯網
上載者:User

標籤: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
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();        }    }}@Testpublic 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.