数据库

 首页 > 数据库 > MySql > java访问mysql数据库的方法

java访问mysql数据库的方法

分享到:
【字体:
导读:
         摘要:java访问mysql数据库的方法 ...

java访问mysql数据库的方法

1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址

2、编程

(1)加载驱动

(2)编程连接操作

(3)返回结果处理


编程示例

import java.sql.*;

public class Access2Database{
	public Connection getConn(){
		Connection conn=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/mytest";
			String user="root";
			String password="111";
			conn=DriverManager.getConnection(url, user, password);
			if(conn!=null){
				System.out.println("The connection to database is successful!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public ResultSet getResultSet(Statement stam,String sql){
		ResultSet res=null;
		try {
			res=stam.executeQuery(sql);
		} catch (SQLException e){
			e.printStackTrace();
		}
		return res;
	}
	void showResultSet(ResultSet res){}
}
import java.sql.*;

public class GetConnection{
	public static void main(String[] args){
		Access2Database adb=new Access2Database();
		Connection conn=adb.getConn();
		Statement stam=null;
		try {
			stam = conn.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		//show resultset
		String sql="select * from student;";
		ResultSet res=adb.getResultSet(stam, sql);
		try {
			System.out.println("nametmajortscore");
			while(res.next()){
				String name,major;
				int score;
				name=res.getString(1);
				major=res.getString(2);
				score=res.getInt(3);
				System.out.println(name+"t"+major+"t"+score);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try{
		res.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		//insert something into table
		sql="insert into student(name,major,score) values('f','Chinese','70');";
		try {
			stam.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//delete something from the table
		sql="delete from student where name='f';";
		try{
			stam.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		//change the data int the table
		sql="update student set score=100 where name='a' and major='Chinese'";
		try{
			stam.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		//prepared statement
		sql="select * from student where name=?";
		PreparedStatement pstam=null;
		try {
			pstam=conn.prepareStatement(sql);
			pstam.setString(1, "a");
			res=pstam.executeQuery();
			System.out.println("**********************");
			while(res.next()){
				String name,major;
				int score;
				name=res.getString(1);
				major=res.getString(2);
				score=res.getInt(3);
				System.out.println(name+"t"+major+"t"+score);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//release the resource of the program
		try{
			res.close();
			pstam.close();
			stam.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
按需调整代码即可

java访问mysql数据库的方法
分享到:
PL-SQL 包的创建和应用
PL-SQL 包的创建和应用 PL-SQL 包的创建和应用 ①简介     包是一组相关过程、函数、变量、常量和游标等PL/SQL程序设计元素的组合,它具有面向对象程序设计语言的特点,是对这些PL/SQL 程序设计元素的封装。 包类似于C++和JAVA语言中的类,其中变量相当于类中的成员变量,过程和函数相当于类方法。 把相关的模块归类...
关系数据库关系的完整性
关系数据库关系的完整性 1、关系:D1×D2×…×Dn的子集叫做在域D1,D2,…Dn上的关系,表示为R(D1,D2,…Dn),R表示关系的名字,n是关系的目或度。关系中的每个元素是关系中的元组。 2、关系模型中有三类完整性约束:实体完整性、参照完整性、用户定义的完整性。其中实体完整性和参照完整性是关系模型必须满足的完整性约束条...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……