Java连接Mysql数据库,并将数据库里面的信息输出到txt文件的基本操作实现
连接数据库:
package com.cn;
import java.sql.*;
public class GetConn {
public Connection conn=null;
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String passWord="123456";
conn=DriverManager.getConnection(url,user,passWord);
if(conn!=null){
System.out.println("数据库连接成功");
}
}
catch(Exception e){
e.printStackTrace();
}
return conn;
}
}
客户信息类
package com.cn;
public class Info {
private String sId;//ID号
private String sName;//姓名
private String sNameId;//身份证号码
private String sInfo;//票的信息
private String sPiao;//票价
private String sFee;//手续费
private String sExtra;//备注
private String sBack;//退款金额
//ID号
public String getsId(){
return sId;
}
public void setsId(String sId){
this.sId=sId;
}
//姓名
public String getsName(){
return sName;
}
public void setsName(String sName){
this.sName=sName;
}
//身份证号码
public String getsNameId(){
return sNameId;
}
public void setsNameId(String sNameId){
this.sNameId=sNameId;
}
//票的信息
public String getsInfo(){
return sInfo;
}
public void setsInfo(String sInfo){
this.sInfo=sInfo;
}
//票价
public String getsPiao(){
return sPiao;
}
public void setsPiao(String sPiao){
this.sPiao=sPiao;
}
//手续费
public String getsFee(){
return sFee;
}
public void setsFee(String sFee){
this.sFee=sFee;
}
//备注
public String getsExtra(){
return sExtra;
}
public void setsExtra(String sExtra){
this.sExtra=sExtra;
}
//退款金额
public String getsBack(){
return sBack;
}
public void setsBack(String sBack){
this.sBack=sBack;
}
}
增加信息
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Add {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
public void add(String id,String name,String nameid,String info,String piao,String fee,String extra,String back){
String sql="select * from ticket where name='"+name+"'";
try{
Statement statement=conn.createStatement();
ResultSet rest=statement.executeQuery(sql);
if(rest.next()){
System.out.println("系统已经存在该用户");
}
else{
String id1=id;
String name1=name;
String nameId1=nameid;
String info1=info;
String piao1=piao;
String fee1=fee;
String extra1=extra;
String back1=back;
statement.executeUpdate("insert into ticket values('"+id1+"','"+name1+"','"+nameId1+"','"+info1+"','"+piao1+"','"+fee1+"','"+extra1+"','"+back1+"')");
conn.close();
System.out.println("添加数据的行数为:"+statement.getUpdateCount());
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
删除信息
package com.cn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Delete {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
public void delete(String name){
String sql="select * from ticket where name='"+name+"'";
try{
Statement statement=conn.createStatement();
ResultSet res=statement.executeQuery(sql);
if(res.next()){
statement.executeUpdate("delete from ticket where name='"+name+"'");
System.out.println("删除成功");
conn.close();
}
else{
System.out.println("要删除的名字不存在");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
将信息输出到TXT文档中
package com.cn;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class FileToTxt {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
String sql="select * from ticket";
File file1=new File("E://text//writeFile.txt");
public void filetotxt(){
try{
Statement statement=conn.createStatement();
ResultSet res=statement.executeQuery(sql);
if(!file1.exists()){
file1.createNewFile();}
FileWriter fileWrite=new FileWriter(file1);
while(res.next()){
String id=res.getString(1);
String name=res.getString(2);
String nameid=res.getString(3);
String info=res.getString(4);
String piao=res.getString(5);
String fee=res.getString(6);
String extra=res.getString(7);
String back=res.getString(8);
//try{
fileWrite.write(id+name+nameid+info+piao+fee+extra+back);
//fw.write("\r\n");
fileWrite.write("\r\n");
// }
// catch(Exception e1){
// e1.printStackTrace();
// }
//System.out.println(id+name+nameid+info+piao+fee+extra+back);
}
fileWrite.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
查找信息
package com.cn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Find {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
// Info info=new Info();
public void find(String name){
String sql="select * from ticket where name='"+name+"'";
try{
PreparedStatement statement=conn.prepareStatement(sql);
ResultSet rest=statement.executeQuery();
while(rest.next()){
//info.setsId(rest.getString(1));
String Id=rest.getString(1);
String Name=rest.getString(2);
String NameId=rest.getString(3);
String Info=rest.getString(4);
String Piao=rest.getString(5);
String Fee=rest.getString(6);
String Extra=rest.getString(7);
String Back=rest.getString(8);
/*info.setsName(rest.getString(2));
info.setsNameId(rest.getString(3));
info.setsInfo(rest.getString(4));
info.setsPiao(rest.getString(5));
info.setsFee(rest.getString(6));
info.setsExtra(rest.getString(7));
info.setsBack(rest.getString(8)); */
String ss=Id+Name+NameId+Info+Piao+Fee+Extra+Back;
System.out.println(ss);
}
}
catch(Exception e){
e.printStackTrace();
}
//String ss=info.getsId()+info.getsName()+info.getsNameId()+info.getsInfo()+info.getsPiao()+info.getsFee()+info.getsExtra()+info.getsBack();
}
}
输出全部信息
package com.cn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Query {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
String sql="select * from ticket";
public void query(){
try{
Statement statement=conn.createStatement();
ResultSet res=statement.executeQuery(sql);
while(res.next()){
String id=res.getString(1);
String name=res.getString(2);
String nameid=res.getString(3);
String info=res.getString(4);
String piao=res.getString(5);
String fee=res.getString(6);
String extra=res.getString(7);
String back=res.getString(8);
System.out.println(id+name+nameid+info+piao+fee+extra+back);
}
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
更新信息
package com.cn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Update {
GetConn getConn=new GetConn();
Connection conn=getConn.getConnection();
public void update(String name,String info){
String sql="select * from ticket where name='"+name+"'";
try{
Statement statement=conn.createStatement();
ResultSet res=statement.executeQuery(sql);
if(res.next()){
statement.executeUpdate("update ticket set info='"+info+"' where name='"+name+"'");
System.out.println("修改成功");
conn.close();
}
else{
System.out.println("要修改的名字不存在");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}