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