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();
}
}
}