`
yaolinnan
  • 浏览: 57052 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
文章分类
社区版块
存档分类
最新评论

java数据库编程

 
阅读更多

1、JDBC

JDBC是由sun公司开发,提供了一种与平台无关的用于执行SQL语句的标准javaAPI,可以方便的实现多种关系型数据库的统一操作。

JDBC最常用的类和接口是DriverManger、Connection、Statement、PreparedStatement和Result。

JDBC的操作步骤分为1.加载数据库驱动程序  2.连接数据库  3.使用语句进行数据库操作   4.关闭数据库连接

 

public class JDBCTest {

	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载mysql数据库驱动类
			Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123456");//获得指定数据库的连接
			PreparedStatement pre=con.prepareStatement("select * from user where name like ? ");
			pre.setString(1, "test");//设置第一个?的参数
			ResultSet result=pre.executeQuery();//执行SQL语句
			while(result.next()){
				System.out.println("编号:"+result.getInt(1));//获得数据库查询的结果
				System.out.println("姓名:"+result.getString(2));
			}
			result.close();
			pre.close();
			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}

 

2、处理大数据对象

在数据库的操作中,我们会遇到需要保存容量比较大的对象,这时我们可以通过CLOB和BLOB两种类型的字段来实现,其中CLOB可以存储海量文字,而BLOB可以存储二进制数据。

下面是通过CLOB来实现大文本数据的存储和读取:

public class CLOBTest {

	public static void main(String[] args) throws SQLException {
		Connection con=getConnection();//获得指定数据库的连接
		saveText(con,"d:"+File.separator+"test.txt");//写入大文本数据
		getText(con,1);//读取数据库中id=1的文本内容
		con.close();
	}
	
	private static Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123456");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static void saveText(Connection con,String url){
		
		String sql="insert into text(title,note) values(?,?)";
		try {
			File file=new File(url);
			InputStream input=new FileInputStream(file);
			PreparedStatement pre=con.prepareStatement(sql);
			pre.setString(1, "java核心技术");
			pre.setAsciiStream(2, input,file.length());//设置输入流和长度
			pre.executeUpdate();
			
			pre.close();
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	private static void getText(Connection con,int id){
		String sql="select * from text where id=?";
		try {
			PreparedStatement pre=con.prepareStatement(sql);
			pre.setInt(1, id);
			ResultSet result=pre.executeQuery();
			while(result.next()){
				String title=result.getString(1);
				InputStream input=result.getAsciiStream(2);
				StringBuffer buffer=new StringBuffer();
				BufferedReader reader=new BufferedReader(new InputStreamReader(input));
				String str="";
				while((str=reader.readLine())!=null){
					buffer.append(str.toString());
				}
				System.out.println("文本的标题:"+title);
				System.out.println("文本的内容:"+buffer.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 BLOB操作和CLOB相似,在mysql数据库中,使用longtext声明的字段是通过BLOB类型来操作,保存是调用setAsciiStream方法,而使用LONGBLOB声明的字段则是通过CLOB类型来操作,保存是调用setBinaryStream方法。

 

3、几种常见数据库的连接方式

 

1)mysql

驱动类--com.mysql.jdbc.Driver

url--jdbc:mysql://localhost:3306/test

 

2)oracle

驱动类--oracle.jdbc.driver.OracleDriver

url--jdbc:oracle:thin:@localhost:1521:test

 

3)SQLServer

驱动类--com.microsoft.sqlserver.jdbc.SQLServerDriver

url--jdbc:sqlserver://localhost:1433;user=sa;password=sa;database=pubs

精彩科技工作室
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics