MySQL--- JDBC
时间:2021-07-01 10:21:17
帮助过:17人阅读
com.zczr.ja01;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
public class JDBC_Test01 {
public static void main(String[] args) {
Connection con =
null;
Statement stat =
null;
ResultSet rs =
null ;
// JDBC - java database connector
try {
// 加载mysql的驱动,并且将mysql的驱动加载到DriverManage类中
Class.forName("com.mysql.jdbc.Driver"
);
// 连接地址
String url = "jdbc:mysql://127.0.0.1:3306/goods"
;
//用户名
String userName = "root"
;
//用户密码
String password = "1234"
;
// 通过驱动管理员获取数据库的连接对象(Connection),要通过的数据库链接地址、数据库用户名、密码
con =
DriverManager.getConnection(url,userName,password);
//编写SQL 语句
String sql = "select * from goodsinfo"
;
//获取sql的执行对象
stat =
con.createStatement();
/**
* 使用sql执行对象来执行已经编写好的sql语句
* 并且返回一个执行结果集(ResultSet)
*/
rs =
stat.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt("gid"
);
String name = rs.getString("gname"
);
String type = rs.getString("gtype"
);
double price = rs.getDouble("price"
);
int num = rs.getInt("num"
);
int order = rs.getInt("oder"
);
String sr = id + " " + name + " " + " " + price + " " + num + " " + order + "\n"
;
System.out.println("查询结果为:\n" +
sr);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs !=
null) {
rs.close();
}
if(stat !=
null) {
rs.close();
}
if(con !=
null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
MySQL--- JDBC
标签:数据 price 执行sql lips statement base 取数 好的 添加