Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mysql.jdbc;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- *
- * @author CCNE
- */
- public class MySQLJDBC {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- //question1();
- //question2("huong", "vu");
- // Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "12345678");
- // java.sql.Statement stmt = con.createStatement();
- // String query = "SELECT * FROM t";
- // ResultSet rs = stmt.executeQuery(query);
- // System.out.print(rs.getMetaData().getColumnCount());
- }
- private static void question1() throws SQLException {
- Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "12345678");
- java.sql.Statement stmt = con.createStatement();
- String query = "SELECT f.title FROM film f JOIN film_actor fa WHERE fa.actor_id = 1 GROUP BY f.title";
- ResultSet rs = stmt.executeQuery(query);
- while(rs.next()) {
- System.out.println("ten phim: " + rs.getString("title"));
- }
- con.close();
- }
- private static void question2(String first_name, String last_name) throws SQLException {
- Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "12345678");
- String query = "INSERT INTO actor (first_name, last_name, last_update) "
- + "value(?, ?, NOW());";
- PreparedStatement pstm = con.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);
- pstm.setString(1, first_name);
- pstm.setString(2, last_name);
- pstm.executeUpdate();
- ResultSet rs = pstm.getGeneratedKeys();
- while(rs.next()) {
- System.out.println("them thanh cong, id: " + rs.getInt(1));
- }
- con.close();
- }
- private static void question4() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement