Original from http://qq163230530.blog.163.com/blog/static/4289250620081186262719/
Eclipse Download after installation
Create a new Web project first configure the server
Configure server-server-tomcat-tomcat7.0 Select Enable to select the path to the Tomcat installation F:\tomcat
Select the following JDK Tianjin Jrehome is the directory where the JDK is installed F:\java
Publish Web project Click Publish to Network Select Project Select tomcat7.0 Publish
Configure the managed user name and password for Tomcat
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<tomcat-users>
<role rolename= "Manager-gui"/>
<role rolename= "Admin-gui"/>
<user username= "admin" password= "" roles= "Manager-gui,admin-gui"/>
</tomcat-users>
Password is blank user name Admin
This article mainly takes MySQL as an example of how Java connects to a database.
Of course, the first thing to install is the JDK (typically jdk1.5.x). Then install MySQL, these are relatively simple, the specific process will not say. After configuring these two environments, download the JDBC driver Mysql-connector-java-5.0.5.zip (This is the latest version). Then unzip it to either directory. I extracted to the D drive, and then added the Mysql-connector-java-5.0.5-bin.jar in its directory to Classpath, as follows: "My Computer", "Properties", "Advanced" "Environment variable", edit classpath in the system variable, add D:\mysql-connector-java-5.0.5\mysql-connector-java-5.0.5-bin.jar to the last, add ";" before adding the string. To separate from the previous Classpath area. And then determine.
The environment is well configured, very simple. Now, configure MySQL with the username "root" and the password "root". Create database on the command line or with a SQL front-end software.
I use SQLyog's front-end software to create database.
Create the database First:
CREATE DATABASE Scutcs;
Next, create the table:
CREATE TABLE STUDENT
(
SNO CHAR (7) Not NULL,
SNAME VARCHAR (8) Not NULL,
SEX CHAR (2) Not NULL,
Bdate DATE not NULL,
HEIGHT DEC (5,2) DEFAULT 000.00,
PRIMARY KEY (SNO)
);
Then insert the data with the SQL statement insert into < table name > values (value1, value2, ...);
You can also use SQLyog to manipulate
All right, I've created it.
Next, let's 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.Driver";
The URL points to the database name you want to access Scutcs
String url = "Jdbc:mysql://127.0.0.1:3306/scutcs";
User name when MySQL is configured
String user = "root";
Password for MySQL configuration
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 used to execute SQL statements
Statement Statement = Conn.createstatement ();
The SQL statement to execute
String sql = "SELECT * from student";
Result set
ResultSet rs = statement.executequery (SQL);
System.out.println ("-----------------");
System.out.println ("Execution results 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");
First, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array.
The specified byte array is then decoded using the GB2312 character set
name = new String (name.getbytes ("iso-8859-1"), "GB2312");
Output results
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 ();
}
}
}
Now let's run a look at the effect:
D:\testjdbc>javac Jdbctest.java
D:\testjdbc>java Jdbctest
Succeeded connecting to the database!
-----------------------
The execution results are as follows:
-----------------------
Name of the study number
-----------------------
0104421 weeks Excursion
0208123 Wang Yiping
0209120 Wang vigorously
0309119 Levi
0309203 Ouyang Merrill
Haha, success.
Java link to MySQL