JDBC入门汇总及范例讲解(三)

2015-07-24 07:47:26 · 作者: · 浏览: 5

?

	public void CreateTableTest(){
		//获取连接
		Connection  cnn2=SqlDB.getconnection();
        Statement statement = null;
		try {
			statement = cnn2.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}     
        //准备SQL语句
		 String sql  ="CREATE TABLE student(sid INT PRIMARY KEY,sname VARCHAR(20),age INT)";
		 System.out.println("CREATE TABLE student......");
        //调用executeQuery执行查询语 
		try {
			statement.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        SqlDB.close(statement,cnn2);		
	}
	
	public void InsertTest(){
		//获取连接
		Connection  cnn2=SqlDB.getconnection();
        Statement statement=null;
		try {
			statement = cnn2.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}  
	     //执行插入操作
        System.out.println("Inserting records into the table...");
        String sql = "INSERT INTO student " +
                     "VALUES (100, '小文', 18)";
        try {
			statement.executeUpdate(sql);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        sql = "INSERT INTO student " +
                     "VALUES (101, '大林', 25)";
        try {
			statement.executeUpdate(sql);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        sql = "INSERT INTO student " +
                     "VALUES (102, '阿白',  30)";
        try {
			statement.executeUpdate(sql);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        sql = "INSERT INTO student " +
                     "VALUES(103, '小小', 28)";
        try {
			statement.executeUpdate(sql);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        System.out.println("Inserted records into the table...");    
        SqlDB.close(statement, cnn2);	
		
	}
		
	

	public void SelectTest(){
		//获取连接
		Connection  cnn2=SqlDB.getconnection();
        Statement statement=null;
		try {
			statement = cnn2.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        //准备SQL语句
        String sql = "select * from student"; 
        //调用executeQuery执行查询语句
        ResultSet res = null;
		try {
			res = statement.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        //查询结束后res会指向表头,想要获取数据必须不断地指向查询结果的下一行,当没有下一行数据时,返回0.
	 System.out.println("select  records from the table...");    
        try {
			while(res.next())
			{
			       //先获取数据
		           int sid  = res.getInt("sid");       
		           String sname = res.getString("sname");
		           int age = res.getInt("age");
		           //打印结果
		           System.out.print("sid: " + sid);
		           System.out.print("  sname: " +sname);
		           System.out.println("  age: " + age);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
        try {
			res.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        SqlDB.close(statement, cnn2);	
		
	}

然后就是调用:

?

?

		JdbcTest jdbcTest=new JdbcTest();
		jdbcTest.CreateTableTest();
		jdbcTest.InsertTest();
		jdbcTest.SelectTest();
结果:

?

\

\

?

发现代码量反而增多了。。。。看来还优化得不好

想办法看是否能把创建数据库、插入数据、查找数据都入在SqlDB之中,然后我们需要时只要传入相应的参数即可,下面就来对SqlDB来进行改造:

把创建数据库、插入和查找都放到一个文件中去

?

package com.mucfc;
import java.sql.*;
/**
 * 数据库操作类的一个简单封装
 * @author 林炳文
 * @time 2015.4.30
 */
public class SqlDB {

	// 定义数据库驱动程序
	private static final String DBDRIVER = "com.mysql.jdbc.Driver";
	// 数据库连接地址
	private