This article mainly to MySQL as an example of how to connect Java to the database, the specific contents are as follows
Of course, the first thing to install is a JDK (typically jdk1.5.x). Then install MySQL, these are relatively simple, the specific process will not say. After configuring both environments, download the JDBC driver Mysql-connector-java-5.0.5.zip (This is the latest version). Then unzip it to any directory. I unpacked to D and then added the Mysql-connector-java-5.0.5-bin.jar to the Classpath, as follows:"My Computer"-> "Properties"-> "Advanced"-> "Environment variables" , edit the classpath in the system variable, add the D:\mysql-connector-java-5.0.5\mysql-connector-java-5.0.5-bin.jar to the end, and add the string before adding the ;", to separate from the former Classpath area. Then determine.
The environment is well configured, very simple. Now, first configure MySQL, set its username "root", the password is "root". Create the database at the command line or with a SQL front-end software.
I use SQLyog front-end software to create the database.
To create a database first:
CREATE DATABASE Scutcs;
Next, create the table:
CREATE TABLE STUDENT
(
SNO char (7) NOT NULL,
sname VARCHAR (8) is not NULL,
SEX char (2) is not NULL,
bdate DA TE not NULL,
HEIGHT DEC (5,2) DEFAULT 000.00,
PRIMARY KEY (SNO)
);
Then insert the data, you can insert into < table name > values (value1, value2, ...) with SQL statements;
You can also use SQLyog to manipulate
All right, here we are.
Next, we'll write a. java file to demonstrate how to access the MySQL database.
Import java.sql.*; public class Jdbctest {public static void main (string[] args) {//driver name String Driver = "Com.mysql.jdbc.Dri
Ver ";
The URL points to the database name to be accessed Scutcs String url = "Jdbc:mysql://127.0.0.1:3306/scutcs";
MySQL configuration user name String user = "root";
MySQL configuration password String password = "root";
try {//Load driver Class.forName (driver);
Continuous database Connection conn = drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ()) System.out.println ("succeeded connecting to the database!");
Statement is used to execute SQL statements Statement Statement = Conn.createstatement ();
SQL statement to execute String sql = "SELECT * from student";
Result set ResultSet rs = statement.executequery (SQL);
System.out.println ("-----------------");
System.out.println ("The results of the execution are as follows:");
System.out.println ("-----------------");
System.out.println ("School Number" + "T" + "name"); System.out.priNtln ("-----------------");
String name = NULL;
while (Rs.next ()) {//select sname This column of data name = Rs.getstring ("sname");
* * Where to ask the hovertree.com/////////////First, use the iso-8859-1 character set to decode name into a byte sequence and store the result in a new byte array
Then use the GB2312 character set to decode the specified byte array name = new String (name.getbytes ("iso-8859-1"), "GB2312");
Output result System.out.println (rs.getstring ("sno") + "\ T" + name);
} rs.close ();
Conn.close ();
catch (ClassNotFoundException e) {System.out.println ("Sorry,can ' t find the driver!");
E.printstacktrace ();
catch (SQLException e) {e.printstacktrace ();
catch (Exception e) {e.printstacktrace ();
}
}
}
Next, we'll run a look at the effect:
D:\testjdbc>javac Jdbctest.java
D:\testjdbc>java jdbctest
succeeded
connecting to the Database! -----------------------
The results of the execution are as follows:
----------------------- name
-----------------------
0104421 Zhou Yuan line
0208123 Wang Yiping
0209120 How to ask
0309119
0309203 Ouyang Merrill Lynch
0309226 Collayi
The above is the Java connection MySQL database detailed steps, I hope to help you learn, but also hope that we support the cloud habitat community.