java连接mysql实现增删改查代码

1.建立mysql

2.下载javasdk

3.下载MyEclipse 并破解。

4.建立一个新的工程,工程属性-》buildpath-》config build path-》把 mysql的驱动加载到目录下

5.上代码,下面的代码实现了java的增删改查,起始数据库也就这些,一些高级的东西,我自从使用数据库以来从来没有用到过。下一篇博文打算使用hibernate来实现增删改查,那就高大上了。。。注:本人c++程序员。

package javaTest1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//import com.mysql.jdbc.Statement;

public class CMain {
	public static void main(String[] args) {
//		MyThread t= new MyThread();
//		t.start();
//		while(true){
//			System.out.println("main ");
//		}
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/hello";
		// MySQL配置时的用户名
		String user = "root";
		// Java连接MySQL配置时的密码
		String password = "501821782";
		try{
			Class.forName(driver);
			Connection connection = DriverManager.getConnection(url,user,password);
			if(!connection.isClosed()){
				System.out.println("yes");
				Statement statement = connection.createStatement();
				String sql = "select * from new_table";
				ResultSet rs = statement.executeQuery(sql);  
				while(rs.next()){
					int id  = rs.getInt("id");
					String string = String.format("%d", id);
					System.out.print(string);;
				}
				//增加
				sql = "insert into new_table (name) VALUES('tt') ";
				statement.execute(sql);
				//更改
				sql = "update new_table set id=27 where name='tt';";
				statement.execute(sql);
				//删除
				sql = "delete from new_table where id = 27";
				statement.execute(sql);
				
			}
		}catch(ClassNotFoundException e) {   
			System.out.println("Sorry,can`t find the Driver!");   
			e.printStackTrace();   
		} catch(SQLException e) {   
			e.printStackTrace();   
		} catch(Exception e) {   
			e.printStackTrace();   
		} 
		
	}
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值