Connect MySQL database with JDBC 2017.5.6

Source: Internet
Author: User

Must be their own heart, need to delve into the things to escape, cheat oneself said to learn other technology, tell yourself to do things, do a good thing, this evening connected to the database, in fact, is also very simple

Note: To add a new driver to the build path path, put it directly in the folder in the Java project in eclipse, can not be used, will error, cannot find the driver

The specific operation of the database SQL statements and Java technology need to continue to learn, since the decision to do computer, it is necessary to adhere to the afternoon, the need for a strong hands-on ability, and thinking, with the wisdom of the brain to think logically, with perseverance to overcome difficulties, adhere to write a blog here, record their ordinary learning process, Become the great God of technology. Come on!

Package db_test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.Statement;

public class Test {

public static void Main (string[] args) {

try {
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();

String databaseName = "phildatabase";//The database has been created in MySQL database.
String userName = "root";//MySQL default root account name
String password = "zhu119829917";//self-defined password
Connection conn = drivermanager.getconnection ("jdbc:mysql://localhost:3306/" + databaseName, userName, password);

Statement stmt = Conn.createstatement ();

String sql = "CREATE TABLE person (uid varchar (+), name char (32))";

Create a table in the database,
int result = stmt.executeupdate (SQL);

if (Result! =-1) {
SYSTEM.OUT.PRINTLN ("Create data Table Success");

sql = "INSERT into person (uid,name) VALUES (' 1 ', ' somebody1 ')";
result = Stmt.executeupdate (SQL);

sql = "INSERT into person (uid,name) VALUES (' 2 ', ' somebody2 ')";
result = Stmt.executeupdate (SQL);

sql = "SELECT * from";

ResultSet rs = stmt.executequery (SQL);
System.out.println ("uid\t name");

while (Rs.next ()) {
System.out.println (rs.getstring (1) + "\ T" + rs.getstring (2));
}
}

Conn.close ();

} catch (Exception e) {
E.printstacktrace ();
}
}
}

Connect MySQL database with JDBC 2017.5.6

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.