java与数据库(在线先等 高手进)
发布网友
发布时间:2024-08-07 05:47
我来回答
共5个回答
热心网友
时间:2024-08-13 08:58
你的这个是插入???? 明明是查询............
给个类似的例子你(学生 的增删查改,数据库MySQL,绝对可以运行,如果你是sql Server 2000数据库的话,就是改下数据库驱动的语句就行了)
学生类(封装学生的信息)
package com.dto;
public class StudentDTO
{
/*定义属性与相应的表中的列名对应*/
private int stuNum;
private String stuName;
private int stuAge;
private String stuSex;
/*生成两个构造器:一个无参,一个全部参数*/
public StudentDTO() {
super();
}
public StudentDTO(int stuNum, String stuName, int stuAge, String stuSex) {
super();
this.stuNum = stuNum;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuSex = stuSex;
}
/*生成Get/Set器*/
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
}
操作类(封装具体的操作)
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dto.StudentDTO;
/*DAO用来封装java针对于数据库的各种操作*/
public class JDBCDAO
{
/*获得所有的记录*/
public List getAll()
{
Connection conn=null;
List<StudentDTO> students=new ArrayList<StudentDTO>();
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
PreparedStatement ps=conn.prepareStatement("select stuNum,stuName,stuAge,stuSex from student");
/*可以替换为:*/
// PreparedStatement ps=conn.prepareStatement("select * from student");
ResultSet rs=ps.executeQuery();
/*重点*/
while(rs.next())
{
StudentDTO student=new StudentDTO();
student.setStuNum(rs.getInt("stuNum"));
student.setStuName(rs.getString("stuName"));
student.setStuAge(rs.getInt("stuAge"));
student.setStuSex(rs.getString("stuSex"));
students.add(student);
}
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return students;
}
/*插入一条记录*/
public void addStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("INSERT into student values (null,?,?,?)");
/*相对应的?按照出现顺序赋值
* 参数1,2,3代表上面问号的位置
* 说明:对第一个问号传一个学生姓名
* 对第二个问号传一个学生年龄
* 对第三个问号传一个学生性别*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*更新一条记录*/
public void upStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("update student set stuName=?,stuAge=?,stuSex=? where stuNum=?");
/*相对应的?按照出现顺序赋值
* 参数1,2,3代表上面问号的位置
* 说明:对第一个问号传一个学生姓名
* 对第二个问号传一个学生年龄
* 对第三个问号传一个学生性别
* 对第四个问号传一个学生学号*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.setInt(4,student.getStuNum());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*删除一条记录*/
public void deleteStudent(int stuNum)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("delete from student where stuNum=?");
/*相对应的?按照出现顺序赋值
* 参数1代表上面问号的位置
* 说明:对stuNum传一个int类型的值*/
ps.setInt(1,stuNum);
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*main里面的测试代码一个一个的执行*/
public static void main(String[] args)
{
/*测试getAll()方法代码*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试addStudent()方法代码*/
// StudentDTO dto=new StudentDTO();
// dto.setStuName("刘菲");
// dto.setStuAge(18);
// dto.setStuSex("女");
// new JDBCDAO().addStudent(dto);
// /*添加后获得所有数据*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试upStudent()方法代码*/
// StudentDTO dto=new StudentDTO();
// dto.setStuNum(1);
// dto.setStuName("牛牛羊");
// dto.setStuAge(18);
// dto.setStuSex("男");
// new JDBCDAO().upStudent(dto);
// /*更新后获得所有数据*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试deleteStudent()方法代码*/
new JDBCDAO().deleteStudent(6);
List<StudentDTO> students=new JDBCDAO().getAll();
for(StudentDTO student:students)
{
System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
}
}
}
热心网友
时间:2024-08-13 08:58
public boolean update(String username,String password) { //你有几个字段你就写几个 你也可以写在一个类里面 写上getter和setter 方法 也就是bean 那就这里就直接写上你的bean,实例化就可以了
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
intrs = stmt.executeUpdate("update user_base set password='"+password+"' where username='"+username+"'"); //这里最好是主键ID
it(rs>0) {
System.out.println(rs.getString("name"));
System.out.println(rs.getInt("passwrd"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
//你上面那个不是插入呢 是查询
public boolean insert(String username,String password) {
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
int rs = stmt.executeUpdate("insert user_base values('"+username+"','"+password+"')");
if(rs>0) {
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
//删除
public boolean view(String username) { //最好是设置一个主键 ID
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
int rs = stmt.executeUpdate(""delete user_base where username='"+username+"'");
if(rs>0{
System.out.println(rs.getString("username"));
System.out.println(rs.getInt("age"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
热心网友
时间:2024-08-13 08:54
滚鸡 巴独子,跟谁装呢
热心网友
时间:2024-08-13 08:56
public int doUpdate(String sql) throws SQLException {
conn = DriverManager.getConnection(url, user, password);
Statement sm = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
int i = sm.executeUpdate(sql);
return i;
}
参数sql 为更新语句 如:update user_base set username="user" where ...
删除一样
热心网友
时间:2024-08-13 08:54
//这是个例子 程序最好自己调试 看看这个
//增、删、改程序
package conndemo;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateText {
public static void main(String args[]){
//final String usql="insert into emp(empno,ename,sal,hiredate) values(1007,'安卫华',1200, to_date('2004-2-12','yyyy-mm-dd'))";
//注意以下String名子必须放在同一行内
//final String csql="create table account(account_number varchar(15) not null,branch_name varchar(15) not null,balance decimal(15,2) not null, primary key(account_number))";
// final String usql="delete from emp";
final String usql="update emp set ename='aaaa' where empno=8889 ";
Connection conn = ConnDemo.getConnection();
try{
Statement stmt = conn.createStatement();
int retValue = stmt.executeUpdate(usql); //适用于insert,update,delete
System.out.println("retrun value=" + retValue);
// stmt.execute(csql); //可适用于create,及其它
conn.close();
}catch(SQLException e){ //以后的catch缺少部分均来自于此
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} } }
System.out.println("End.");
}
}
热心网友
时间:2024-08-13 08:55
你的这个是插入???? 明明是查询............
给个类似的例子你(学生 的增删查改,数据库MySQL,绝对可以运行,如果你是sql Server 2000数据库的话,就是改下数据库驱动的语句就行了)
学生类(封装学生的信息)
package com.dto;
public class StudentDTO
{
/*定义属性与相应的表中的列名对应*/
private int stuNum;
private String stuName;
private int stuAge;
private String stuSex;
/*生成两个构造器:一个无参,一个全部参数*/
public StudentDTO() {
super();
}
public StudentDTO(int stuNum, String stuName, int stuAge, String stuSex) {
super();
this.stuNum = stuNum;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuSex = stuSex;
}
/*生成Get/Set器*/
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
}
操作类(封装具体的操作)
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dto.StudentDTO;
/*DAO用来封装java针对于数据库的各种操作*/
public class JDBCDAO
{
/*获得所有的记录*/
public List getAll()
{
Connection conn=null;
List<StudentDTO> students=new ArrayList<StudentDTO>();
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
PreparedStatement ps=conn.prepareStatement("select stuNum,stuName,stuAge,stuSex from student");
/*可以替换为:*/
// PreparedStatement ps=conn.prepareStatement("select * from student");
ResultSet rs=ps.executeQuery();
/*重点*/
while(rs.next())
{
StudentDTO student=new StudentDTO();
student.setStuNum(rs.getInt("stuNum"));
student.setStuName(rs.getString("stuName"));
student.setStuAge(rs.getInt("stuAge"));
student.setStuSex(rs.getString("stuSex"));
students.add(student);
}
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return students;
}
/*插入一条记录*/
public void addStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("INSERT into student values (null,?,?,?)");
/*相对应的?按照出现顺序赋值
* 参数1,2,3代表上面问号的位置
* 说明:对第一个问号传一个学生姓名
* 对第二个问号传一个学生年龄
* 对第三个问号传一个学生性别*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*更新一条记录*/
public void upStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("update student set stuName=?,stuAge=?,stuSex=? where stuNum=?");
/*相对应的?按照出现顺序赋值
* 参数1,2,3代表上面问号的位置
* 说明:对第一个问号传一个学生姓名
* 对第二个问号传一个学生年龄
* 对第三个问号传一个学生性别
* 对第四个问号传一个学生学号*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.setInt(4,student.getStuNum());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*删除一条记录*/
public void deleteStudent(int stuNum)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重点*/
PreparedStatement ps=conn.prepareStatement("delete from student where stuNum=?");
/*相对应的?按照出现顺序赋值
* 参数1代表上面问号的位置
* 说明:对stuNum传一个int类型的值*/
ps.setInt(1,stuNum);
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*main里面的测试代码一个一个的执行*/
public static void main(String[] args)
{
/*测试getAll()方法代码*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试addStudent()方法代码*/
// StudentDTO dto=new StudentDTO();
// dto.setStuName("刘菲");
// dto.setStuAge(18);
// dto.setStuSex("女");
// new JDBCDAO().addStudent(dto);
// /*添加后获得所有数据*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试upStudent()方法代码*/
// StudentDTO dto=new StudentDTO();
// dto.setStuNum(1);
// dto.setStuName("牛牛羊");
// dto.setStuAge(18);
// dto.setStuSex("男");
// new JDBCDAO().upStudent(dto);
// /*更新后获得所有数据*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*测试deleteStudent()方法代码*/
new JDBCDAO().deleteStudent(6);
List<StudentDTO> students=new JDBCDAO().getAll();
for(StudentDTO student:students)
{
System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
}
}
}
热心网友
时间:2024-08-13 08:55
public int doUpdate(String sql) throws SQLException {
conn = DriverManager.getConnection(url, user, password);
Statement sm = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
int i = sm.executeUpdate(sql);
return i;
}
参数sql 为更新语句 如:update user_base set username="user" where ...
删除一样
热心网友
时间:2024-08-13 08:55
滚鸡 巴独子,跟谁装呢
热心网友
时间:2024-08-13 09:01
public boolean update(String username,String password) { //你有几个字段你就写几个 你也可以写在一个类里面 写上getter和setter 方法 也就是bean 那就这里就直接写上你的bean,实例化就可以了
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
intrs = stmt.executeUpdate("update user_base set password='"+password+"' where username='"+username+"'"); //这里最好是主键ID
it(rs>0) {
System.out.println(rs.getString("name"));
System.out.println(rs.getInt("passwrd"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
//你上面那个不是插入呢 是查询
public boolean insert(String username,String password) {
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
int rs = stmt.executeUpdate("insert user_base values('"+username+"','"+password+"')");
if(rs>0) {
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
//删除
public boolean view(String username) { //最好是设置一个主键 ID
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
int rs = stmt.executeUpdate(""delete user_base where username='"+username+"'");
if(rs>0{
System.out.println(rs.getString("username"));
System.out.println(rs.getInt("age"));
return true;
} else{
return false;
}
} catch (Exception e) {
}
return false;
}
热心网友
时间:2024-08-13 08:54
//这是个例子 程序最好自己调试 看看这个
//增、删、改程序
package conndemo;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateText {
public static void main(String args[]){
//final String usql="insert into emp(empno,ename,sal,hiredate) values(1007,'安卫华',1200, to_date('2004-2-12','yyyy-mm-dd'))";
//注意以下String名子必须放在同一行内
//final String csql="create table account(account_number varchar(15) not null,branch_name varchar(15) not null,balance decimal(15,2) not null, primary key(account_number))";
// final String usql="delete from emp";
final String usql="update emp set ename='aaaa' where empno=8889 ";
Connection conn = ConnDemo.getConnection();
try{
Statement stmt = conn.createStatement();
int retValue = stmt.executeUpdate(usql); //适用于insert,update,delete
System.out.println("retrun value=" + retValue);
// stmt.execute(csql); //可适用于create,及其它
conn.close();
}catch(SQLException e){ //以后的catch缺少部分均来自于此
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} } }
System.out.println("End.");
}
}