JDBC1

 

JDBC使用步骤
//1.导入驱动程序
//2.加载数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库联接(数据库地址,数据库用户名,数据库密码)
//MySQL数据库的URL→jdbc:mysql://localhost:3306/mydb
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu", "root", "root");
		
//4.获得Statement对象:操作数据库(insert update delete select)
Statement state= conn.createStatement();
		
//5.执行SQL语句
//String sql = "INSERT INTO t_user(username,`password`) VALUES('mysql','000000')";
//String sql = "update t_user set username = 'Apple' where id = 10";
String sql = "delete from t_user where id = 6";
		
int rows = state.executeUpdate(sql);//executeUpdate()方法用于执行insert,update,delete语句不能用于查询
ResultSet set=state.executeQuery(sql);   //查询
while(set.next()){
	int id=set.getInt("id");
	String name=set.getString("name");
	System.out.println(id+"\t"+name);
}
//6.释放联接
state.close();
conn.close();

 

 

例子一:数据库的增删改查

 

public class Test1 {

	public static void main(String[] args) {
		Connection con=null;
		Statement state=null;
		int result=0;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/stu","root","root");
			state=con.createStatement();
			
			//增
			//String sql="insert into stu(`name`,age,address) values('rose',50,'UK')";
			//删
			String sql="delete from stu where id=1";
			//改
			//String sql="update stu set `name`='lucy' where `name`='rose'";
			result=state.executeUpdate(sql);
			System.out.println(result);
			
			//查
			String sqlString="select id,`name`,age,address from stu";
			ResultSet set=state.executeQuery(sqlString);
			
			while (set.next()) {
				int id=set.getInt("id");
				String name=set.getString("name");
				int age=set.getInt("age");
				String address=set.getString("address");
				
				System.out.println(id+"\t"+name+"\t"+age+"\t"+address);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (state!=null) {
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if (con!=null) {
						try {
							con.close();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
		
	}

}

 

 

例子二:这种方法是建立一个DBHelper类,能实现增删改的功能,不能实现查,只需在其他类里调用即可。public class DBHelper {

 
	private final String DRIVER="com.mysql.jdbc.Driver";
	private final String URL="jdbc:mysql://localhost:3306/stu";
	
	private Connection getConnection() throws Exception{
		Class.forName(DRIVER);
		return DriverManager.getConnection(URL,"root","root");
	}
	
	public int execute(String sql){
		Connection connection=null;
		Statement state=null;
		int result=0;
		
		try {
			connection=getConnection();
			state=connection.createStatement();
			result=state.executeUpdate(sql);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close(connection,state);
		}
		
		return result;
	}

	private void close(Connection connection, Statement state) {
		// TODO Auto-generated method stub
		if (state!=null) {
			try {
				state.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if (connection!=null) {
					try {
						connection.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
	
}
 

当要使用时,在测试类QueryTest里测试即可。public class QueryTest {

	public static void main(String[] args) {
		String sqlString="insert into stu(`name`,age,address) values('DMC',23,'CA')";
		DBHelper helper=new DBHelper();
		int i= helper.execute(sqlString);
		System.out.println(i);
	}

}
最后上知识点图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值