日期:2014-05-16  浏览次数:20547 次

JDBC简单实现

本着实用性的原创,去除JDBC一些深入的东西。只是为了一个很好复习和或者入门,一个总结。

public class TestJDBC {
	
	private static Connection connection = null;
	
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		connection = getConnection();
		Statement statement = connection.createStatement();
		//建表
		//statement.execute("create table mytable(id integer,name varchar(20),password varchar(20))");
		//插入数据
		//statement.execute("insert into mytable values(1,'name','name')");
		//更新
		//statement.execute("update mytable set name='lll' where id=1");
		//查询表内容
		/*
		ResultSet set = statement.executeQuery("select * from mytable");
		ResultSetMetaData meta = set.getMetaData();
		
		while(set.next()){
			for(int i=1;i<=meta.getColumnCount();i++){
				Object o = set.getString(i);
				System.out.print(o+" ");
			}
			System.out.println();
		}*/
		//删除
		//statement.execute("delete from mytable where id=1");
		//删除表
		//statement.execute("drop table mytable");
		
		statement.close();
		connection.close();
		
	}
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","");
	}

}