myeclipse通过jdbc 连接mysql数据库

1.前提是MyEclipse已经能正常开发Java工程

2.安装MySQL 

3.下载JDBC驱动,会用到下面这个包


4.主目录如下,新建web04工程,将上图的包复制到工程中,add buildpath.

myeclipse通过jdbc 连接mysql数据库_第1张图片

5. util.DB.java用来 实现jdbc连接mysql数据库

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DB {    //jdbc 连接mysql数据库
	
	private static Connection conn = null;
	
	public static Connection getConn(){
		
		
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载mysql jbbc的数据库驱动
			String url = "jdbc:mysql://localhost:3306/school";
			String user = "root";
			String password = "root";
			try {
				//取得连接的url,能访问MySQL数据库的用户名,密码,数据库名 
				conn = DriverManager.getConnection(url, user, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
}

6.Dao.StudentDao.java用来创建于mysql连接的类的实例

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import model.Student;
import util.DB;

public class StudentDao {

	public void add(Student student) { // 将student对象作为数据库中的一条记录添加进去

		// 得到一个mysql数据库的一个连接
		Connection conn = DB.getConn();
		PreparedStatement ptmt = null;
		// 通过conn对象得到statement对象
		try {

			String sql = "insert into student(name,schoolname,tel) values(?,?,?)";
			ptmt = conn.prepareStatement(sql);

			// 对sql语句中的占位符进行具体值的设置
			ptmt.setString(1, student.getName());
			ptmt.setString(2, student.getSchoolName());
			ptmt.setString(3, student.getTel());

			ptmt.execute();

			System.out.println("添加学生信息成功了。。。");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (ptmt != null) {
			try {
				ptmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

7.Student.java.实现对student各个属性的赋值

package model;

public class Student {
	private int id;
	private String name;
	private String schoolName;
	private String tel;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSchoolName() {
		return schoolName;
	}
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	
}

8.StudentDaoTest.java 实例化StudentDao,Student 类并赋值

package Dao;

import model.Student;

public class StudentDaoTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		StudentDao studentDao = new StudentDao();
		Student student = new Student();
		student.setName("熊五");
		student.setSchoolName("河北经贸大学");
		student.setTel("110");
		studentDao.add(student);

	}

}

9.执行 StudentDaoTest.java。成功给数据库school中的student表添加了信息


你可能感兴趣的:(java,web)