當前位置:首頁 » 數據倉庫 » eclipse如何配置mysql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

eclipse如何配置mysql

發布時間: 2022-02-26 07:58:05

『壹』 如何為eclipse配置mysql資料庫

使用Eclipse連接到MySQL資料庫需要使用到JDBC
JDBC: (Java DataBase Connectivity Java資料庫連接)
JDBC是一種用於執行SQL語句的Java的API. 可以為多種關系型資料庫提供統一的訪問. 它是由一組使用Java語言編寫的類或介面組成.

JDBC開發步驟
1. 搭建開發環境, 引入資料庫驅動(導入資料庫驅動的jar包)
相關jar包: mysql-connector-java-5.0.8-bin.jar
2. 具體代碼實現
public void demo1() throws SQLException(){
//載入驅動

Class.forName("com.mysql.jdbc.Driver");

//獲得連接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/資料庫名稱","登錄名","登錄密碼");

//編寫SQL並執行SQL

String sql = "select * from user"; //需要執行的sql語句

Statement statement = connection.createStatement();

//結果集 -- 執行查詢數據,其他操作需另寫代碼

ResultSet rs = statement.executeQuery(sql);

//遍歷 列印獲得的數據
while(rs.next()){

int id = rs.getInt("id");

String username =
rs.getString("username");

String password = rs.getString("password");

System.out.println(id+" "+username+" "+password);

}
// 4.釋放資源.
rs.close();
statement.close();
connection.close();
}

『貳』 如何用eclipse連接mysql資料庫

1。MySQL安裝,安裝完居後
下面來創建一個數據:
mysql>CREATE DATABASE test; //創建一個資料庫
mysql>use test; //指定test為當前要操作的資料庫
mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //創建一個表user,設置兩個欄位。
mysql>INSERT INTO user VALUES('huheng','123456'); //插入一條數據到表中

2。打開Eclipse,創建一個項目(my),
操作:右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅動,點擊確定。
我的項目列表:
3。驅動已經導入,下面我們來寫一個程序驗證一下
import java.sql.*;
publicclass MysqlJdbc {
publicstaticvoid 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","198876");
//連接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!
huheng
出現上面結果,說明你連接資料庫成功。
4。可以查看到MySQL裡面的內容,那我們是不是想往MySQL中插入數據呢。
下面的例子,往MySQL的user表中插入100條數據
import java.sql.*;
publicclass Myjproject {
publicstaticvoid 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","198876");
int num=100;
PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");
for(int i=0;i<num;i++) //定義個100次的循環,往表裡插入一百條信息。
{
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資料庫進行查看
mysql> show databases; //查看所資料庫
mysql> use test; //使test為當前要操作的資料庫
mysql> show tables; //查看當前資料庫的所有表
mysql> select *from user; //查看當前表(user)的所有信息

『叄』 eclipse怎麼連mysql資料庫

  1. 在新建的Project中右鍵新建Floder

  2. 創建名為lib的包

  3. 接下來解壓你下載的mysql的jar包,拷貝其中的.jar文件

  4. 在工程lib包下右鍵選擇paste即粘貼,把mysql的jar包拷貝進來

  5. 在mysql的jar包上右鍵選擇 build path - add to build path

  6. 添加完畢之後,工程才與Mysql的jar包關聯起來,現在可以使用相關類和方法了

  7. 在工程中新建JdbcTest1.java類

  8. http://jingyan..com/article/3aed632e1a4ceb70108091f6.html

『肆』 eclipse怎麼連接mysql

今天介紹的是eclipse如何與MySQL相連,相信很多小夥伴和我一樣,對路徑啊,什麼包放在那裡啊很是頭疼,今天一下午才弄好就趕來分享啦,超詳細哦!

准備工作:下載MySQL和jdbc,這里就不講了,注意的是你可能已經下載了MySQL,但是沒有下載jdbc,它們的下載是分開的,所以你可以先看看本文後面的一些步驟,先確定自己有沒有下載jdbc,沒下載的去官網下載一下,只有幾兆,非常快。

1、首先打開eclipse,新建一個工程「My」,具體操作為:

點擊「File」----點擊「new」----點擊「java project」,得到下圖頁面,在project name填工程名字,比如「My」,然後點擊「finish」

2、這個時候在左邊我們可以看到工程已經被創建出來了

右鍵「My」——「build path」——「configure build path」

在libraries這里點擊郵編第二個「add external jars」,找到你當時下載jdbc後把它解壓縮存的位置,找到MySQL jar打開

3、點擊apply和apply and close即可

4、點擊My,點擊rreferenced libraries界面如下,你看到那個jar包就行了

5、打開MySQL客戶端,輸入你自己的密碼,show databases;查看自己的資料庫有哪些

6、接下來用程序測試一下src——右鍵new——class——填寫name:jdbc,把最下面的第一個也打鉤,然後finish

7、代碼如下

import java.sql.*;
public class Jdbc {
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","java");//java這個空填寫的是你自己設的密碼

System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
//user 為你表的名稱,可以在MySQL命令行用show tables;顯示
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}

8、正常這樣是不會出錯的,但是由於版本問題可能會出錯

如果出現這種錯誤,則:(1)

(2) "jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true","root","java");//java這個空填寫的是你自己設的密碼改這兩處就行了,希望對大家有幫助呀!
————————————————
版權聲明:本文為CSDN博主「資本主義向茶葵」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_40996041/article/details/82502712

『伍』 eclipse 怎麼和mysql資料庫連接

工具:

eclipse

步驟



1、導入jdbc驅動包。點擊菜單欄中的Windows→preferences。

『陸』 eclipse 怎麼配置資料庫連接

1打開Eclipse,找到「Window」--「Show View」--「Other...」

2在彈出的新窗口中,打開文件夾「Data Management」找到「Data Source Explorer」,並單擊「OK」。

3這時候,Eclipse主界面下方會多出一個「Data Source Explorer」標簽欄,在其中「Database Connections」文件夾圖標上單擊右鍵,選中「New...」。

4出現新窗口,找到自己正在使用的資料庫"Oracle",自行起個名字,小編在此起了"cityinfo"這個名字。然後單擊「Next>」

5出現如下窗口時,請單擊右上方圖中所示的符號(位置),注意此步驟。

緊接上步,在新的對話框中,找到你所使用的oracle版本,建議選用Oracle Thin Driver,靈活,方便。

6找到JAR List,若大家使用的是oracle11,請將其中的ojdbc14給remove掉,否則,Eclipse會不停提示出錯。

『柒』 如何在eclipse配置mysql資料庫

如果使用框架的話,在對應的框架中的配置文件裡面配置就好,網上有很多開源的東西。
如果僅使用JDBC的話。

JDBC連接資料庫
•創建一個以JDBC連接資料庫的程序,包含7個步驟:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驅動程序類 ,載入驅動失敗!");
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例注冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,
該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和
密碼來獲得。
例如:
//連接MySql資料庫,用戶名和密碼都是root
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "root" ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("資料庫連接失敗!");
se.printStackTrace() ;
}
4、創建一個Statement
•要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3
種類型:
1、執行靜態SQL語句。通常通過Statement實例實現。
2、執行動態SQL語句。通常通過PreparedStatement實例實現。
3、執行資料庫存儲過程。通常通過CallableStatement實例實現。
具體的實現方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt =
con.prepareCall("{CALL demoSp(? , ?)}") ;
5、執行SQL語句
Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate
和execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句
,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或
DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的
語句。
具體實現的代碼:
ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
int rows = stmt.executeUpdate("INSERT INTO ...") ;
boolean flag = stmt.execute(String sql) ;
6、處理結果
兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
• ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些
行中數據的訪問。
• 使用結果集(ResultSet)對象的訪問方法獲取數據:
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1) ; // 此方法比較高效
}
(列是從左到右編號的,並且從列1開始)
7、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲
明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連接對象
if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

『捌』 eclipse如何連接mysql

public Connection getConnection()
{
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("資料庫驅動載入成功!");
String url="jdbc:mysql://localhost:3306/dbname"; //dbname改為你資料庫的實際名字
Stirng user="root"; //root改成你實際的資料庫登錄名
String passWord="123"; //123 改為實際的密碼
conn=DriverManager.getConnection(url,user,passWord);
System.out.println("mysql資料庫已連接成功!");
}catch(Exception e){
e.printStackTrace();
}
}
別忘了把mysql的jdbc包加進去

『玖』 如何用Eclipse連接MySQL資料庫

一、在MySql中創建資料庫,並創建表,向表中插入數據

1、創建資料庫

createdatabaseselect_test

2、創建表

createtableteacher_table(

Id int,

NameVarchar(20),

Sex Varchar(2)

)

3、向表中插入數據(這里插入三條測試數據)

insertintoteacher_table values(1,'zhangsan','ma');

insertintoteacher_table values(2,'lisi','fe');

insertintoteacher_table values(3,'wangwu','ma');

二、配置Eclipse。

配置之前請先下載mysql-connector-java-5.1.15-bin.jar文件。

右鍵單擊包所在的工程包(project),Build Path ---> Configure Build Path,在彈出的窗口中選擇 Add External JARs。把你下載並解壓出來的mysql-connector-java-5.1.15-bin.jar選中。如圖

資料庫名:select_test

用戶名:root

密碼:123456

連接成功後顯示teacher_table表中的數據。

import java.sql.*;

class ConnMySql {

/**

* @param args

* @throws Exception

*/

publicstaticvoid main(String[] args) throws Exception {

// TODO Auto-generated method stub

Class.forName("com.mysql.jdbc.Driver");

Connectionconn = DriverManager.getConnection(

"jdbc:mysql://127.0.0.1:3306/select_test",

"root","123456");

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select * from teacher_table");

while (rs.next()) {

System.out.println(rs.getInt(1) + " "

+rs.getString(2) + " "

+rs.getString(3) );

}

if (rs != null) {

rs.close();

}

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}

}