Java??MySQL?????????????????????
???????????? ???????[ 2016/11/7 10:21:19 ] ????????????????? Java
????0. ???????
????(1) ????Class.forName()????????????????
????(2) ????DriverManager?????getConnection()????????????Connection????
????(3) ???????Statement??????????SQL??????SQL????????Statement??????????е???????PreparedStatement??????????????CallableStatement?????洢?????????????
????(4) ????excuteQuery()????????SQL????????????????ResultSet???????????executeUpdate()????????SQL?????????ResultSet?????????
????(5)??????ResultSet????????????????????
????(6)????????
????1. ?????????
????(1) ????MySQL????????
?????????http://dev.mysql.com/downloads/connector/j/??????????F:??????????????????mysql??????????У??????
????(2) ????JDBC????
????????????????Eclipse?У????????????????Project-Properties?е?Java Build Path????Libraries??????mysql-connector-Java-5.1.21-bin.jar????OK??
????(3) ?????????????????£?
????import java.sql.*;
????public class GetConnection {
????public static void main(String[] args){
????try{
????//????Class.forName()????????????????
????Class.forName("com.mysql.jdbc.Driver");
????System.out.println("???????MySQL??????");
????}catch(ClassNotFoundException e1){
????System.out.println("?????MySQL????!");
????e1.printStackTrace();
????}
????String url="jdbc:mysql://localhost:3306/mysql"; //JDBC??URL
????//????DriverManager?????getConnection()????????????Connection????
????Connection conn;
????try {
????conn = DriverManager.getConnection(url?? "root"??"");
????//???????Statement????
????Statement stmt = conn.createStatement(); //????Statement????
????System.out.print("?????????????");
????stmt.close();
????conn.close();
????} catch (SQLException e){
????e.printStackTrace();
????}
????}
????}
????2. ????????
?????????????????????ResultSet?????????????????????????y???????????ü?????????????????????????????
????Java??MySQL?????????????????????
????import java.sql.*;
????public class SelectTable {
????public static void main(String[] args){
????try{
????//????Class.forName()????????????????
????Class.forName("com.mysql.jdbc.Driver");
????System.out.println("???????MySQL??????");
????String url="jdbc:mysql://localhost:3306/aniu"; //JDBC??URL
????Connection conn;
????conn = DriverManager.getConnection(url?? "root"??"");
????Statement stmt = conn.createStatement(); //????Statement????
????System.out.println("?????????????");
????String sql = "select * from stu"; //???е?SQL
????ResultSet rs = stmt.executeQuery(sql);//???????????
????System.out.println("???"+" "+"????"+" "+"????");
????while (rs.next()){
????System.out.print(rs.getInt(1) + " ");
????System.out.print(rs.getString(2) + " ");
????System.out.print(rs.getInt(3) + " ");
????System.out.println();
????}
????rs.close();
????stmt.close();
????conn.close();
????}catch(Exception e)
????{
????e.printStackTrace();
????}
????}
????}
????3. ????????????
????//??????????
????import java.sql.*;
????public class UpdateDeleteDemo {
????public static void main(String[] args)throws Exception{
????try{
????//????Class.forName()????????????????
????Class.forName("com.mysql.jdbc.Driver");
????System.out.println("???????MySQL??????");
????String url="jdbc:mysql://localhost:3306/aniu"; //JDBC??URL
????Connection conn;
????conn = DriverManager.getConnection(url?? "root"??"");
????Statement stmt = conn.createStatement(); //????Statement????
????System.out.println("?????????????");
????//???????????
????String sql = "select * from stu"; //???е?SQL
????ResultSet rs = stmt.executeQuery(sql);//???????????
????System.out.println("???"+" "+"????"+" "+"????");
????while (rs.next()){
????System.out.print(rs.getInt(1) + " ");
????System.out.print(rs.getString(2) + " ");
????System.out.print(rs.getInt(3) + " ");
????System.out.println();
????}
????//???????????
????String sql2 = "update stu set name=? where number=?";
????PreparedStatement pst = conn.prepareStatement(sql2);
????pst.setString(1??"8888");
????pst.setInt(2??198);
????pst.executeUpdate();
????//???????????
????String sql3 = "delete from stu where number=?";
????pst = conn.prepareStatement(sql3);
????pst.setInt(1??701);
????pst.executeUpdate();
????ResultSet rs2 = stmt.executeQuery(sql);//???????????
????System.out.println("???"+" "+"????"+" "+"????");
????while (rs.next()){
????System.out.print(rs2.getInt(1) + " ");
????System.out.print(rs2.getString(2) + " ");
????System.out.print(rs2.getInt(3) + " ");
????System.out.println();
????}
????rs.close();
????stmt.close();
????conn.close();
????}catch(Exception e)
????{
????e.printStackTrace();
????}
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11