如何用JAVA连接MySQL

 准备:安装MySQL数据库,下载JDBChttp://dev.mysql.com/downloads/connector/j/,JAVA环境

附:JDBC官方说明文档:http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

java连接数据库包:

1.Eclipse_luna,打开菜单file->import->General->Existing Projects....导入java连接数据库包

2.JDBC数据库,scr->Bulid Path->Libraries->Add External...导入JDBC 解压后的mysql-connector-java-5.1.38-bin.jar文件

3.一个Student数据库,代码如下

create database student;

use student;
DROP TABLE IF EXISTS `st`;
CREATE TABLE `st` (
  `学号` char(6) NOT NULL,
  `姓名` varchar(8) NOT NULL,
  `性别` varchar(2) default NULL,
  `出生日期` date default NULL,
  `专业` varchar(20) default NULL,
  `总学分` decimal(4,1) default NULL,
  `照片` blob,
  `备注` text,
  PRIMARY KEY  (`学号`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

4.修改java测试类

testListAccess.java

package listaccess;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class TestListAccess {
 Connection con;    
 Statement st;
 List list=new ArrayList();
 public static void main(String[] args) {
  TestListAccess mytest=new TestListAccess();
  mytest.query();    //查询记录 
     System.out.println("查询结果为:");
     Collections.sort(mytest.list, new StuOrderByBirth());
     mytest.show();    
 }
 public Connection getConnection() {
  Connection tmpCon = null;
  String sDBDriver="com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://127.0.0.1:3306/student"; //这里连接的是student数据库
  String username="root";//数据库用户名
  String password="888888";//数据库密码
     try {
      Class.forName(sDBDriver);
      tmpCon = DriverManager.getConnection(url,username,password);
     } catch (Exception e) {
      System.out.println("这里数据库连接失败" + e.getMessage());
         e.printStackTrace();
     }
     return tmpCon;
 } 
 public void query() {
  con = getConnection(); 
  try { 
            String sql = "select * from st";
            st = (Statement) con.createStatement();             
            ResultSet rs = st.executeQuery(sql);           
            while (rs.next()) {
             Student stu=new Student();
             stu.setStuid(rs.getString("学号"));
             stu.setName(rs.getString("姓名")); 
             stu.setSex(rs.getString("性别"));
             stu.setBirthdate(rs.getDate("出生日期").toString());
//             stu.setHometown(rs.getString("籍贯"));
//             stu.setScore(rs.getInt("总分"));
                list.add(stu);                             
            } 
            con.close();              
      } catch (SQLException e) {
       System.out.println("查询数据失败"); 
      } 
    } 
 public void show() {
  Iterator iter=list.iterator();
  while (iter.hasNext()){
   System.out.println(iter.next());
  }  
 }
}

Student.java

package listaccess;

public class Student {
 String stuid;
 String name;
 String sex;
 String birthdate;
 String hometown;
 int score;
 public String getStuid() {
  return stuid;
 }
 public void setStuid(String stuid) {
  this.stuid = stuid;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getBirthdate() {
  return birthdate;
 }
 public void setBirthdate(String birthdate) {
  this.birthdate = birthdate;
 }
// public String getHometown() {
//  return hometown;
// }
// public void setHometown(String hometown) {
//  this.hometown = hometown;
// }
// public int getScore() {
//  return score;
// }
// public void setScore(int score) {
//  this.score = score;
// }
// public String toString(){
//  return stuid+"  "+name+"   "+sex+"  "+birthdate+"  "+hometown+"  "+score;
// }
 public String toString(){
  return stuid+"  "+name+"   "+sex+"  "+birthdate;
 }

}

最后运行即可!