java中怎样创建MySQL数据库列表 需要使用jdbc访问数据库。
具体步骤如下:
1:加载驱动 ,返回连接
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/student"
private static final String DATABASE_USRE = "root"
private static final String DATABASE_PASSWORD = "cs"
/**
* 返回连接
*
* @return Connection
*/
public static Connection getConnection() {
Connection dbConnection = null
try {
Class.forName(DRIVER_CLASS)
dbConnection = DriverManager.getConnection(DATABASE_URL,
DATABASE_USRE, DATABASE_PASSWORD)
} catch (Exception e) {
e.printStackTrace()
}
return dbConnection
}
2:获取连接,执行sql语句
public static List selectAllStudent() {
Connection con = null
PreparedStatement pstm = null
ResultSet rs = null
List list = new ArrayList()
String sql = "select * from Student "
try {
con = ConnectionManager.getConnection()
pstm = con.prepareStatement(sql)
// pstm.setString(1, uname)
rs = pstm.executeQuery()
while (rs.next()) {
Student model = new Student()
model.setStuId(rs.getInt("stuId"))
list.add(model)
}
} catch (SQLException e) {
e.printStackTrace()
} finally {
ConnectionManager.closeResultSet(rs)
ConnectionManager.closeStatement(pstm)
ConnectionManager.closeConnection(con)
}
return list
}
3:调用获取列表方法。
集合运算
这是一种二目运算,一共有四种四种运算符:并,差,交,笛卡尔积;
语法:
查询语句
[UNION | UNIONALL | INTERSECT | MINUS]
查询语句
UNION(并集)
返回若干个查询结果,但是重复的不显示
Eg:SELECT *FROM dept
UNION
SELECT*FROM dept WHERE deptno = 10
注:查询 *** 作编写的过程中尽量使用UNION , UNION ALL代替 OR,提高查询速度
例:
查询工作是销售和clerk的;
SELECT*
FROM emp WHEREjob = ‘saleman’ or job = ‘clerk’
另一种方式:
SELECT * FROMemp WHERE job = ‘SALESMAN’
UNION
SELECT * FROMemp WHERE job = ‘CLERK’
UNION ALL(并集)
返回若干个查询结果,但是重复的也显示
Eg:SELECT * FROM dept
UNION ALL
SELECT*FROM dept WHERE deptno = 10
MINUS(差集)
返回若干个结果中不同的部分;
Eg:SELECT * FROM dept
MINUS
SELCT*FROM dept WHERE deptno = 10
INTERSECT(交集)
显示查询结果中相同的部分;
Eg:SELECT * FROM dept
INTERSECT
SELCT*FROM dept WHERE deptno = 10
————————————————
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)