数据库最基础操作---java课程
写出程序连接并访问数据库,查询并输出商品号,对数据进行增删改查
配置环境变量:
在Eclipse开发环境中,将驱动程序文件作为外部库添加到项目。右击项目名称,在弹出的快捷菜单中选择Properties命令,在打开
的窗口左侧选择 Java Build Path,在右侧Libraries选项卡右侧选择 Add External JARs~~按钮,在打开的对话框找到驱动程式打包文件。
数据库建立:
Navicat Premium 从 locahost中建立数据库,从中创建表格,可以直接修改信息或用SQL语句添加信息;
| id | pname | price | |
| 103 | '电脑' | 5500.0 | |
| 107 | '手机' | 2000.0 | |
| 104 | '耳机' | 1500.0 |
操作:
try{
Class,forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
String sql="insert into products values(?,?,?)";//?为要操作的对象
//String sql="delete from products where id=?"
//String sql="updata products set price=? where id=?"
//String sql="select * from products where id=?"//若是全部查询,将 where id =? 删除
try(
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/webstore","root", "sa123");//sa123为访问密码
PreparedStatement st=con.prepareStatement(sql);){
//开始操作
//对每个❓进行一步步操作;
st.setInt(1, 106);
st.setString(2, "平板电脑");
st.setDouble(3, 1500.00);
st.setInt(4,106);
.........
//检测操作是否成功
int i=st.executeUpdate();
if(i==1) {
System.out.println("success.");
}else {
System.out.println("fail.");
}
//遍历查询
ResultSet rs = st.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("id") + "\t" + rs.getString("pname") +
"\t\t" + rs.teDouble("price") );
}
}catch(SQLException e) {
e.printStackTrace();
}
访问数据库,查询并输出 商品号104一下的数据,具体代码:
package sss;
import java.sql.*;
public class Ass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException cne) {
cne.printStackTrace();
}
String dburl = "jdbc:mysql://localhost:3306/webstore";//webstore数据库名字
String sql = "SELECT * FROM products WHERE id < 104";// where id < 4 为选择条件
try (Connection conn
= DriverManager.getConnection(dburl, "root", "sa123");//sa123为密码
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery(sql))
{
while (rst.next()) {//遍历
System.out.println(rst.getInt(1)+"\t"+rst.getString(2)+"\t" +rst.getDouble(3)+"\t");
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
增删改查分开的四种操作:
1 //增加数据 2 package sss; 3 import java.sql.*; 4 public class Insert { 5 public static void main(String[] args) { 6 try{ 7 Class.forName("com.mysql.jdbc.Driver"); 8 }catch(ClassNotFoundException e){ 9 e.printStackTrace(); 10 } 11 String sql="insert into products values(?,?,?)"; 12 try(Connection con=DriverManager.getConnection( 13 "jdbc:mysql://localhost:3306/webstore","root", "sa123"); 14 PreparedStatement st=con.prepareStatement(sql); 15 ){ 16 st.setInt(1, 106); 17 st.setString(2, "平板电脑"); 18 st.setDouble(3, 1500.00); 19 int i=st.executeUpdate(); 20 if(i==1) { 21 System.out.println("success."); 22 }else { 23 System.out.println("fail."); 24 } 25 26 }catch(SQLException e) { 27 e.printStackTrace(); 28 } 29 } 30 } 31 //删除数据 32 package sss; 33 import java.sql.*; 34 public class Delete { 35 public static void main(String[] args) { 36 try{ 37 Class.forName("com.mysql.jdbc.Driver"); 38 }catch(ClassNotFoundException e){ 39 e.printStackTrace(); 40 } 41 String sql="delete from products where id=?"; 42 try(Connection con=DriverManager.getConnection( 43 "jdbc:mysql://localhost:3306/webstore","root", "sa123"); 44 PreparedStatement st=con.prepareStatement(sql); 45 ){ 46 st.setInt(1, 102); 47 int i=st.executeUpdate(); 48 if(i==1) { 49 System.out.println("success."); 50 }else { 51 System.out.println("fail."); 52 } 53 54 }catch(SQLException e) { 55 e.printStackTrace(); 56 } 57 } 58 } 59 //修改数据 60 package sss; 61 import java.sql.*; 62 public class Update { 63 public static void main(String[] args) { 64 try{ 65 Class.forName("com.mysql.jdbc.Driver"); 66 }catch(ClassNotFoundException e){ 67 e.printStackTrace(); 68 } 69 String sql="update products set price=? where id=?"; 70 try(Connection con=DriverManager.getConnection( 71 "jdbc:mysql://localhost:3306/webstore","root", "sa123"); 72 PreparedStatement st=con.prepareStatement(sql); 73 ){ 74 st.setDouble(1, 4000.00); 75 st.setInt(2, 103); 76 int i=st.executeUpdate(); 77 if(i==1) { 78 System.out.println("success."); 79 }else { 80 System.out.println("fail."); 81 } 82 83 }catch(SQLException e) { 84 e.printStackTrace(); 85 } 86 } 87 } 88 //查询数据 89 package sss; 90 import java.sql.*; 91 public class Select { 92 public static void main(String[] args) { 93 try{ 94 Class.forName("com.mysql.jdbc.Driver"); 95 }catch(ClassNotFoundException e){ 96 e.printStackTrace(); 97 } 98 String sql="select * from products where id=?";//查询所有的记录 把where id = ? 删除 99 try(Connection con=DriverManager.getConnection( 100 "jdbc:mysql://localhost:3306/webstore","root", "sa123"); 101 PreparedStatement st=con.prepareStatement(sql); 102 ){ 103 st.setInt(1, 102); 104 ResultSet rs=st.executeQuery(); 105 //遍历查询 106 while(rs.next()) { 107 System.out.println(rs.getInt("id")+"\t" 108 +rs.getString("pname")+"\t\t"+rs.getDouble("price")); 109 } 110 rs.close(); 111 }catch(SQLException e) { 112 e.printStackTrace(); 113 } 114 } 115 }

浙公网安备 33010602011771号