<output id="r87xx"></output>
    1. 
      
      <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
        •   

               當前位置:首頁>軟件介紹>MySQL數(shù)據(jù)庫連接 查詢:
               
          MySQL數(shù)據(jù)庫連接

                  我的環(huán)境:MySQL:mysql-essential-5.1.51-win32 jdbc驅(qū)動:我已經(jīng)上傳到csdn上一個:

                  Eclipse:任意版本,免費的,可以百度的到。

                  1。MySQL安裝,不會的朋友可以看連接:下面來創(chuàng)建一個數(shù)據(jù):

                  mysql>CREATE DATABASE test; //創(chuàng)建一個數(shù)據(jù)庫

                  mysql>use test; //指定test為當前要操作的數(shù)據(jù)庫

                  mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //創(chuàng)建一個表user,設(shè)置兩個字段。

                  mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一條數(shù)據(jù)到表中

                  2。打開Eclipse,創(chuàng)建一個項目(my),

                  操作:右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅(qū)動,點擊確定。

                  

                  我的項目列表:

                  

                  3。驅(qū)動已經(jīng)導(dǎo)入,下面我們來寫一個程序驗證一下

                  import java.sql.*;

                  public class MysqlJdbc {

                  public static void main(String args[]) {

                  try {

                  Class.forName("com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅(qū)動程序

                  //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","198876");

                  //連接URL為 jdbc:mysql//服務(wù)器地址/數(shù)據(jù)庫名 ,后面的2個參數(shù)分別是登陸用戶名和密碼

                  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!

                  huzhiheng

                  出現(xiàn)上面結(jié)果,說明你連接數(shù)據(jù)庫成功。

                  4。可以查看到MySQL里面的內(nèi)容,那我們是不是想往MySQL中插入數(shù)據(jù)呢。 下面的例子,往MySQL的user表中插入100條數(shù)據(jù)

                  import java.sql.*;

                  public class Myjproject {

                  public static void main(String args[])

                  {

                  try {

                  Class.forName("com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅(qū)動程序

                  //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","198876");

                  int num=100;

                  PreparedStatement Statement=connect.prepareStatement("INSERT INTO user

                  VALUES(?,?)");

                  for(int i=0;i<num;i++) //定義個100次的循環(huán),往表里插入一百條信息。

                  {

                  Statement.setString(1,"chongshi"+i);

                  Statement.setString(2,"bo"+i);

                  Statement.executeUpdate();

                  }

                  // } catch (ClassNotFoundException e) {

                  // TODO Auto-generated catch block

                  // System.out.println("An error has occurred:"+e.toString());

                  // e.printStackTrace();

                  }catch(SQLException e)

                  {

                  }

                  }

                  }

                  5.下面我們打開MySQL數(shù)據(jù)庫進行查看

                  mysql> show tatabases; //查看所數(shù)據(jù)庫

                  mysql> use test; //使test為當前要操作的數(shù)據(jù)庫

                  mysql> show tables; //查看當前數(shù)據(jù)庫的所有表

                  mysql> select *from user; //查看當前表(user)的所有信息

                  

                  注意:如果不能正常連接你的數(shù)據(jù)庫,請檢查你代碼中,驅(qū)動、用戶名、密碼、表等信息是否對應(yīng)無誤,不要把別人的代碼直接復(fù)制過來,看也不看就用。 

                  


          php陰歷程序寶典php驗證碼制作
          PHP的頁面緩沖處理機制PHP之防御sql注入攻擊的方式
          PHP入門篇php教程-基礎(chǔ)版
          PHP搜索附近的人實現(xiàn)代碼PHP的FTP學(xué)習(xí)
          php網(wǎng)站如何防止sql注入PHP語言的有哪些優(yōu)勢和特點
          php代碼優(yōu)化關(guān)于php測試部署和持續(xù)集成
          常見的MySQL命令大全MySQL索引讓MySQL更快速運行
          MySQL使用教程MySQL快速入門
          信息發(fā)布:廣州名易軟件有限公司 http://www.jetlc.com
          • 勁爆價:
            不限功能
            不限用戶
            1998元/年

          • 微信客服

            <output id="r87xx"></output>
          1. 
            
            <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
              • 国产乱子伦-区二区三区熟睡91 | 亚洲男女激情91免费网站 | 夜夜撸综合 | 蜜桃视频成人 免费 | 国产福利视频在线 | 男女操逼逼 | 乱伦综合肉 | 爱爱午夜福利 | 日欧美在线视频 | 欧美抄逼视频播放 |