import java.sql.Driver
import java.sql.DriverManager
import java.sql.SQLException
import java.util.Properties
public clss DBTest{
public DBTest(){
//加载驱动
try{
class.forName("com.mysql.jdbc.Driver")
//设置数据库的访问路径
String url = "jdbc:mysql://localhost:3306//derby"
//设置用户名和密码
Properties info = new Properties()
info.setProperty("user","zhanghao")
info.setProperty("password","mima")
//获取连接
Connection conn = DriverManager.getConnection(url,info)
Statement stmt = conn.createStatement()
//组织查询语句
String sql = "select * form table_name"
//获取查询结果集合
ResultSet result = stmt.excuteQuery(sql)
//遍历结果,假设table_name中第一列为id,类型int
while(result.next()){
System.out.println(result.getInt(1))
}
}catch(SQLException){
}
catch(Exception e){
// e.printStackTrace()
}finally{
//最后关闭资源
result.close()
stmt.close()
conn.close()
}
}
}
//现场手打 可能会有点下错误 你自己测试下 大致流程就是这样的
String sql = "select * from reader where readerId=? and readerPwd=?"PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setString(1,user)
pstmt.setString(2,pass)
ResultSet rs = pstmt.executeQuery()
if(rs.next()){
flag = true
}
String sql = "select count(*) from reader where readerId=? and readerPwd=?"
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setString(1,user)
pstmt.setString(2,pass)
ResultSet rs = pstmt.executeQuery()
rs.next()
if(rs.getInt()!=0){
flag = true
}
可以使用百度Hi示意我有时间可以解决你的问题
差不多的要求也可以示意我
ES:\\F6603BD412A7BECDC45FD1EABF1F4B02
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)