数据库内容
java源代码 (代码实现的是查询成绩为 的人员信息 至于其他功能的 代码中有注释) 注意 在eclipse里运行程序的时候 要工程里插入jar包 否则运行异常! import java sql *import java io *class database_manage { public Connection conn = null public ResultSet rs = null private String DatabaseDriver = " mysql jdbc Driver" // DataSource 数据源名称DSN private String DatabaseConnStr = "jdbc:mysql://localhost: /people_manage?useUnicode=true&characterEncoding=utf " + " root root" public void setDatabaseDriver(String Driver) { this DatabaseDriver = Driver } public String getDatabaseDriver() { return (this DatabaseDriver) } public void setDatabaseConnStr(String ConnStr) { this DatabaseConnStr = ConnStr } public String getDatabaseConnStr() { return (this DatabaseConnStr) } public database_manage() {// 构造函数连接数据库 try { Class forName(DatabaseDriver)} catch (java lang ClassNotFoundException e) { System err println("加载驱动器有错误:" + e getMessage()) System out print("执行插入有错误:" + e getMessage())// 输出到客户端 } } public ResultSet query(String sql) {// 查询数据库 rs = nulltry { conn = DriverManager getConnection( "jdbc:mysql://localhost: /people_manage?useUnicode=true&characterEncoding=utf " "root" "root") Statement stmt = conn createStatement() rs = stmt executeQuery(sql)} catch (SQLException ex) { System err println("执行查询有错误:" + ex getMessage()) System out print("执行查询有错误:" + ex getMessage())// 输出到客户端 }return rs
} public int update_database(String sql) {// 更新或插入数据库 int num = try { conn = DriverManager getConnection( "jdbc:mysql://localhost: /people_manage?useUnicode=true&characterEncoding=utf " "root" "root") Statement stmt = conn createStatement() num = stmt executeUpdate(sql)} catch (SQLException ex) { System err println("执行插入有错误:" + ex getMessage()) System out print("执行插入有错误:" + ex getMessage())// 输出到客户端 } CloseDataBase()return num } public void CloseDataBase() {// 关闭数据库 try { conn close()} catch (Exception end) { System err println("执行关闭Connection对象有错误 " + end getMessage()) System out print("执行执行关闭Connection对象有错误 有错误:" + end getMessage())// 输出到客户端 } } } class people { private String uid private String name private String banji private int score public people() { } public people(String uid String name String banji) { this uid = uidthis name = namethis banji = banji } public people(String uid String name String banji int score) { this uid = uidthis name = namethis banji = banjithis score = score } public String getUid() { return uid } public void setUid(String uid) { this uid = uid } public String getName() { return name } public void setName(String name) { this name = name } public String getBanji() { return banji } public void setBanji(String banji) { this banji = banji } public int getScore() { return score } public void setScore(int score) { this score = score } } public class manage { private people[] people_array // 对象数组 public void add_people(String uid String name) { String sql = "insert people (uid name) values ( " + uid + " " + name + " )"// sql插入语句 // String sql = "insert people (uid name) values ( uid tom )" database_manage db_obj = new database_manage()db_obj update_database(sql) } public void update_people(String uid String name) { String sql = "update people set name= " + name + " where uid= " + uid + " "database_manage db_obj = new database_manage()db_obj update_database(sql) } public void delete_people(String uid) { String sql = "delete from people where uid= " + uid + " " database_manage db_obj = new database_manage() db_obj update_database(sql) } public people query_people(String uid) { database_manage db_obj = new database_manage()// String adminid=nullString uid_new name banjiuid_new = nullname = nullbanji = nullString sql_query = "select * from people where uid= " + uid + " " try { ResultSet rs = db_obj query(sql_query) if (rs next()) { uid_new = rs getString("uid")name = rs getString("name")banji = rs getString("banji") } } catch (Exception e) { e getMessage()} people new_people = new people(uid_new name banji)return new_people } public people[] query_people_byscore(int score) { database_manage db_obj = new database_manage()String uid_new name banjiuid_new = nullname = nullbanji = nullint score_new =String sql_query = "select * from people where score=" + score// sql查询语句
try {
ResultSet rs = db_obj query(sql_query)// 查询后 返回结果集
int num =
ResultSet rs_new = rs
while (rs_new next()) {// 统计结果集中学生个数
num++
}
// System out println(num)
people_array = new people[num]
int i =
rs beforeFirst()// 返回结果集的开始
while (rs next()) {
uid_new = rs getString("uid")
name = rs getString("name")
banji = rs getString("banji")
score_new = rs getInt("score")
people_array [i] = new people(uid_new name banji score_new)
i++
}
} catch (Exception e) {
e getMessage()
}
return people_array
}
public static void main(String args[]) {
/*
* people new_people=new people()
*
* manage mr=new manage()//mr add_people(" " "小明")插入一个学生的信息
*
* new_people=mr query_people(" ")//查询uid= 的学生信息 返回对象 System out
* println(""+new_people getName()+" "+new_people getBanji())
* mr update_people(" " "小王")更新一个学生的信息
*
* new_people=mr query_people(" ")//更新后查询
*
* System out println(""+new_people getName()+" "+new_people getBanji(
* ))
*/
manage mr = new manage()
// mr delete_people(" ")删除uid= 的学生信息
people[] people_array// 声明对象数组
people_array = mr query_people_byscore( )// 返回成绩为一百的学生类数组 后输出
int num =
num = people_array length
for (int i = i <numi++) {
System out println(people_array[i] getUid() + " "
+ people_array[i] getName() + " "
+ people_array[i] getBanji() + " "
+ people_array[i] getScore())
}
}
}
lishixinzhi/Article/program/Java/JSP/201311/200301.安装好jre环境和和jdk,设置好环境变量,很基础,网上教程很多;
2.安装mysql数据库,不用设置数据源和环境变量,只是安装好就可以,具体过程见网上教程,有时候不好安装,可能是因为之前安装没有卸载干净等原因,多查一查怎么弄,多试一试就成功了;
3.为了方便 *** 作数据库,为mysql安装一个可视化界面Navicat,安装过程超级简单,使用也超级简单,傻瓜 *** 作;
4.怎么连接java和mysql呢,这个搞了好久,网上下载一个压缩包mysql-connector-java-5.0.8.zip,解压,将其中的mysql-connector-java-5.0.8-bin.jar文件复制一份粘贴在C:\Program
Files\Java\jre7\lib\ext目录下,也就是放在jre\lib\ext下就行;
5.接下来还要设置一次环境变量,第4步中的压缩包解压后,将解压后文件中mysql-connector-java-5.0.8-bin.jar添加到环境变量classpath中;
第1步 获取Java与MySQL的连接器第2步 将下载的包存放在自己机器上jdk安装目录下面的jre目录下面的lib目录下面的ext目录中,在程序中导入与数据库 *** 作相关的对象
import java.sql.Connection//导入数据库连接对象
import java.sql.DriverManager //导入数据库驱动管理对象
import java.sql.ResultSet //导入数据记录集对象
import java.sql.SQLException //导入数据SQL *** 作异常对象
import java.sql.Statement //导入SQL *** 作接口对象
第3步:在程序中写入如下内容
String url//连接数据库的字符串
String sql//执行数据sql查询 *** 作的字符串
Connection conn//数据库连接变量
Statement st//数据库 *** 作对象
ResultSet rs//数据记录集对象
url = "jdbc:mysql://localhost:3306/test?user=root&password="//在程序中只要修改这句,就可以实现数据库连接
try {
conn = DriverManager.getConnection(url)
st = conn.createStatement()
sql = "select * from test"//只要修改这句,就可以实现各种查询 *** 作
rs=st.executeQuery(sql)//执行数据查询
while(rs.next())
{
System.out.println(rs.getString(1))//获得数据表test中第1个字段的数据,该字段为字符串类型
System.out.println(rs.getString(2))//获得数据表test中第2个字段的数据,该字段为字符串类型
}
rs.close()//关闭数据记录集
conn.close() //关闭数据库连接
} catch (SQLException e) {
System.out.println("Error:"+e.toString()+e.getMessage())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)