码迷,mamicode.com
首页 > 数据库 > 详细

Java - 面向对象思想进行JDBC编程

时间:2017-03-05 20:45:22      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:pack   resource   XML   pre   etag   rom   1.7   input   access   

mysql-connector-java-5.1.7-bin.jar

jdbc.properties

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\://localhost\:3306/lesson
user=root
password=

JDBCTools.java

package 面向对象思想进行JDBC编程;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Driver;
import com.mysql.jdbc.Statement;

public class JDBCTools {
	public static void update(String sql)
	{
		Connection conn=null;
		java.sql.Statement statement=null;
		try {conn=getConnection();
		statement= conn.createStatement();
		statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{JDBCTools.release(conn, (Statement) statement);
		}
	}
	public static Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException
	{
		String driverClass=null;
		String jdbcUrl=null;
		String user=null;
		String password=null;
		InputStream in= JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
		Properties p=new Properties();
		p.load(in);
		driverClass=p.getProperty("driver");
		jdbcUrl=p.getProperty("jdbcUrl");
		user=p.getProperty("user");
		password=p.getProperty("password");
		Driver driver=(Driver) Class.forName(driverClass).newInstance();
		Properties info=new Properties ();
		info.put("user",user);
		info.put("password",password);
		Connection connection=driver.connect(jdbcUrl, info);
		System.out.println(connection);
		return connection;	
	}
	public static void release(Connection conn,Statement statement)
	{

		try {
			if(statement!=null)
				statement.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}

Student.java

public class Student {
	
		private int id;	
		private String name;
		private int age;
		private int clas;
		private String address;
		private String sex;
		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 int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public int getClas() {
			return clas;
		}
		public void setClas(int clas) {
			this.clas = clas;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public String getSex() {
			return sex;
		}
		public void setSex(String sex) {
			this.sex = sex;
		}
		public Student(int id, String name, int age, int clas, String address,
				String sex) {
			super();
			this.id = id;
			this.name = name;
			this.age = age;
			this.clas = clas;
			this.address = address;
			this.sex = sex;
		}
		public Student() {
			super();
		}
		@Override
		public String toString() {
			return "Student [id=" + id + ", name=" + name + ", age=" + age
					+ ", clas=" + clas + ", address=" + address + ", sex=" + sex
					+ "]";
		}
		
}

test.java

import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;


public class test {
//	public void testAddnewStudent() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException
//	{
//		
//		testAddNew(test.getInformationFromConsole());
//	}
	private static Student getInformationFromConsole()
	{
		Scanner sc=new Scanner(System.in);
	Student stu=new Student();
	System.out.println("Id:");
	stu.setId(sc.nextInt());
	System.out.println("Name:");
	stu.setName(sc.next());
	System.out.println("Age:");
	stu.setAge(sc.nextInt());
	System.out.println("Clas:");
	stu.setClas(sc.nextInt());
	System.out.println("Address:");
	stu.setAddress(sc.next());
	System.out.println("Sex:");
	stu.setSex(sc.next());
		return stu;
		
	}
	public void testAddNew(Student stu) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
		String sql="INSERT INTO study VALUES("+stu.getId()+",‘"+stu.getName()+"‘,"+stu.getAge()+","+stu.getClas()+",‘"+stu.getAddress()+"‘,‘"+stu.getSex()+"‘)";
    System.out.println(sql);
		JDBCTools.update(sql);
    
	}
	public static void main(String[] args) {
		test te=new test();
		
		try {
			te.testAddNew(te.getInformationFromConsole());
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Java - 面向对象思想进行JDBC编程

标签:pack   resource   XML   pre   etag   rom   1.7   input   access   

原文地址:http://www.cnblogs.com/anstoner/p/6506569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!