eclipse串連mysql,eclipsemysql

來源:互聯網
上載者:User

eclipse串連mysql,eclipsemysql

1. 首先安裝mysql資料庫,具體安裝方法可以上百度搜下,也可以按照下面的方法來安裝mysql:

安裝Mysql:

下載xampp,安裝;

開啟xampp 控制台,啟動mysql

把Mysql的執行檔案路徑添加到PATH環境變數中來

Mysql安裝完成


2. 下載mysql的串連jar包:mysql-connector-java-5.1.22-bin.jar

http://download.csdn.net/detail/liujan511536/8972159

3. 啟動mysql後,在命令列中進入mysql的root使用者:

mysql -u root

然後建立資料庫:

create database db;

建立表user:

use db;create table user(id int, name varchar(20));insert into user(id, name) values (1, 'hello');insert into user(id, name) values(2, 'good');


4. 在eclipse中建立Java Project,然後向該工程中添加mysql-connector-java-5.1.22-bin.jar;

5. 接著在剛才的工程中建立類Conn,並添加以下代碼:

Conn.java:

import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.Connection;public class Conn {public static void main(String[] args) throws ClassNotFoundException, SQLException{Class.forName("com.mysql.jdbc.Driver");Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root", "");Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from user");while(rs.next()){System.out.println(rs.getInt(1) + " " + rs.getString(2));}if (rs != null)rs.close();if (stmt != null)stmt.close();if (conn != null)conn.close();}}

運行該工程就可以串連到資料庫了。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

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.