Eclipse 串連MySql資料庫總結

來源:互聯網
上載者:User

Eclipse 串連MySql資料庫總結

一、在MySql中建立資料庫,並建立表,向表中插入資料

1、建立資料庫

create database select_test

2、建立表

create table teacher_table(    Id int,    Name Varchar(20),    Sex Varchar(2)    )

3、向表中插入資料(這裡插入三條測試資料)

insert into teacher_table values(1,'zhangsan','ma');insert into teacher_table values(2,'lisi','fe');insert into teacher_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  */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubClass.forName("com.mysql.jdbc.Driver");Connection conn = 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) + "\t"+rs.getString(2) + "\t"+rs.getString(3) );}if (rs != null) {rs.close();}if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}}}

 總結:Elipse串連MySql資料庫要求下載mysql-connector-java-5.1.15-bin.jar檔案。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.