java数据库编程
启动MySQL服务,有两种方式
1.cmd 中输入 services.msc打开服务窗口
2.管理员启动cmd 输入 net start mysql80
操作数据库:
方式1:进入MySQL, 在命令行中输入密码; 方式2:在命令行中:找到安装目录下的bin录制中有个mysql的命令 格式:mysql -u账户 -p密码 -h数据库服务器安装的主机 -P数据库端口 mysql -uroot -padmin -hlocalhost -P 3306 若连接的数据库服务器在本机上,并且端口是3306。
java连接数据库
package java数据库编程;
import java.sql.Statement;
public class ConnectJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//断口路径
final String URL = "jdbc:mysql:///test";
final String USER = "root";
final String PASSWORD="root";
//在mysql新的版本中已经不需要加载驱动程序
//Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println(conn);
//接口需要通过Connection接口进行实例化
Statement stmt = conn.createStatement();
String sql = "create table students(id int not null auto_increment,"
+ "name varchar(20) not null,"
+ "class_id int not null,"
+ "score smallint,"
+ "primary key(id) )";
String sql1 = "insert into students(name,class_id,score) values('小明',1,99),('小华',2,88);";
// stmt.executeUpdate("drop table students;");
// stmt.executeUpdate(sql);
// stmt.executeUpdate(sql1);
// stmt.executeUpdate("update students set name='小强',class_id = 2,score=90 where id =2");
// ResultSet result = stmt.executeQuery("select id ,name ,score from students;");
// while(result.next()) {
// int id = result.getInt("id");
// String name = result.getString("name");
// int score = result.getInt("score");
// System.out.print("id="+id);
// System.out.print(";name="+name);
System.out.println(";score="+score);
// int id = result.getInt(1);
// String name = result.getString(2);
// int score = result.getInt(3);
// System.out.println(id +" " +name+" " +score);
// }
String sql2="insert into students(name,class_id,score)values(?,?,?)";
//预处理具有更好的灵活性
PreparedStatement pstmt = conn.prepareStatement(sql2);
pstmt.setString(1,"刘晟");//设置第一个?号的内容
pstmt.setInt(2,3); //设置第二个?号的内容
pstmt.setInt(3,99); //...
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
批处理
public class Batch {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//断口路径
final String URL = "jdbc:mysql:///test";
final String USER = "root";
final String PASSWORD="root";
//在mysql新的版本中已经不需要加载驱动程序
//Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println(conn);
//接口需要通过Connection接口进行实例化
Statement stmt = conn.createStatement();
String sql = "create table students(id int not null auto_increment,"
+ "name varchar(20) not null,"
+ "class_id int not null,"
+ "score smallint,"
+ "primary key(id) )";
String sql1 = "insert into students(name,class_id,score) values('小明',1,99),('小华',2,88);";
String sql2="insert into students(name,class_id,score)values(?,?,?)";
//预处理具有更好的灵活性
//批处理
//Statement接口上定义一个addBach()方法,此方法可以加入批处理
PreparedStatement pstmt = conn.prepareStatement(sql2);
for(int i= 0 ;i<10;i++) {
pstmt.setString(1, "student"+'1');
pstmt.setInt(2,i%2);
pstmt.setInt(3,new Random().nextInt(100));
pstmt.addBatch();//增加批处理
}
pstmt.executeBatch();//批处理
pstmt.close();
conn.close();
}
}
事务处理
public class Commit {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//断口路径
final String URL = "jdbc:mysql:///test";
final String USER = "root";
final String PASSWORD="root";
//在mysql新的版本中已经不需要加载驱动程序
//Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
//取消自动提交
conn.setAutoCommit(false);
//接口需要通过Connection接口进行实例化
Statement stmt = conn.createStatement();
String sql2="insert into students(name,class_id,score)values(?,?,?)";
PreparedStatement stmt1 = conn.prepareStatement(sql2);
for(int i= 0 ;i<10;i++) {
stmt1.setString(1, "student"+'1');
stmt1.setInt(2,i%2);
stmt1.setInt(3,new Random().nextInt(100));
stmt1.addBatch();//增加批处理
}
stmt1.executeBatch();//批处理
try {
conn.commit();
}catch(Exception e) {
conn.rollback();
}
stmt1.close();
conn.close();
}
}