<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    eclipse 怎么連接數據庫 MySQL


    發現錯別字 2年前 提問
    回答
    1
    瀏覽
    7746
    請勿發布不友善或者負能量的內容。與人為善,比聰明更重要!
    回答數量: 1
    等保高級測評師 NISP

    1.創建數據庫、表格、具體值

    mysql>CREATE   DATABASE test;   //創建一個數據庫
    
    mysql>use  test;  //指定test為當前要操作的數據庫
    
    mysql>CREATE  TABLE  user (name VARCHAR(20),password VARCHAR(20));   //創建一個表user,設置兩個字段。
    
    mysql>INSERT  INTO  user  VALUES('jacob','050818'); //插入一條數據到表中
    

    2.打開Eclipse,創建一個項目(my)。

    操作:右鍵點擊my—>build Path—>add external Archiver…選擇jdbc驅動,點擊確定。

    圖片

    我的項目列表:

    圖片

    3.具體Java代碼如下:(注意:代碼不能直接用,需要你的用戶名和密碼)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class MysqlJdbc {
    public static void main(String args[]) {
        try {
            Class.forName("com.mysql.jdbc.Driver");     //加載MYSQL JDBC驅動程序   
            //Class.forName("org.gjt.mm.mysql.Driver");
            System.out.println("Success loading Mysql Driver!");
            } catch (Exception e) {
                System.out.print("Error loading Mysql Driver!");
                e.printStackTrace();
            }
            try {
                Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root",
                        "050818" + "");
                // 連接URL為 jdbc:mysql//服務器地址/數據庫名 ,后面的2個參數分別是登陸用戶名和密碼
    
                System.out.println("Success connect Mysql server!");
                Statement stmt = connect.createStatement();
                ResultSet rs = stmt.executeQuery("select * from user");
                // user 為你表的名稱
                while (rs.next()) {
                    System.out.println(rs.getString("name"));
                }
        }catch (Exception e) {
            System.out.print("get data error!");
            e.printStackTrace();
        }
      }
    }

    點擊運行程序:

    Success loading Mysql Driver!
    
    Success connect Mysql server!
    
    jacob

    出現上面結果,說明你連接數據庫成功。

    下面的例子,往MySQL的user表中插入100條數據

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    
    
    
    public class Myproject {
    
    
        public static void main(String[] args) {
            try{
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("Success loading MySQL Drive");
            }catch(Exception e){
                System.out.println("Error loading MySQL Driver!");
                e.printStackTrace();
            }
            try{
                Connection connect=DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","050818");
                int num=100;
                PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");
                for(int i=0;i<num;i++){    
                    Statement.setString(1,"chongshi"+i);
                    Statement.setString(2,"bo"+i);
                    Statement.executeUpdate();
                }
    
    
               }catch(SQLException e){}
        }
    }
    

    下面我們打開MySQL數據庫進行查看

    mysql> show databases;  //查看所數據庫
    mysql> use  test;    //使test為當前要操作的數據庫
    mysql> show tables; //查看當前數據庫的所有表
    mysql> select *from user;  //查看當前表(user)的所有信息

    回答所涉及的環境:聯想天逸510S、Windows 10。

    2年前 / 評論
    亚洲 欧美 自拍 唯美 另类