Eclipse Connection MySQL

Source: Internet
Author: User
Tags stmt

1. Download the driver.

2.eclipse->add extend jars, add driver.

3. Test:

Build databases and tables in MySQL, and operate the database in eclipse.

Code:

Mysql:

Create Database test;create table user (name, varchar (), password varchar); INSERT into user values (' Xiaotao ', ' 1234 56 ');

Java (View and add Records):

Package Sql;import Java.lang.reflect.executable;import java.sql.*;p ublic class Connector {public static void main ( String[] args) {try {class.forname ("com.mysql.jdbc.Driver"); System.out.println ("Success loading Mysql Driver");} catch (Exception e) {System.out.println ("Error loading Mysql driver!"); E.printstacktrace ();} try {Connection connect = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456"); System.out.println ("Success connect Mysql server!"); Statement stmt = Connect.createstatement (); ResultSet rs = stmt.executequery ("Select *from user"), while (Rs.next ()) {System.out.println (rs.getstring ("name"));} PreparedStatement stmt2 = connect.preparestatement ("INSERT into user value (?,?)"); Stmt2.setstring (1, "Sunhongtao"); Stmt2.setstring (2, "123"); Stmt2.executeupdate ();} catch (Exception e) {System.out.println ("Get Data error!"); E.printstacktrace ();}}}

Eclipse Connection MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.