如何在Java程序中访问mysql数据库中的数据并进行简单的操作
可以参考:
java连接Mysq数据库,以及增删查改
package aocis;
import java.sql.*;
impor tjavax.swing.*;
/**
*
* @author acer
*/
publicclassMysql {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
publicMysql()
{
try{
//这里使用的是Mysql
Class.forName("org.gjt.mm.mysql.Driver");//驱动,
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/scutcs?useUnicode=true&characterEncoding=gbk","root","123");//连接数据库,其中?useUnicode=true&characterEncoding=gbk这句是解决mysql数据库插入中文的
stmt = con.createStatement();
}catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"未加载jdbc驱动n"+e.toString());
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库语法错误n"+e.toString());
}
}
publicvoidsearch(String s) //数据库查询
{
try
{
rs = stmt.executeQuery(s);
}catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"执行查询语句出错n"+e.toString());
}
}
publicintinsert(String s) //数据库插入
{
try{
returnstmt.executeUpdate(s);
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"执行插入语句错误n"+e.toString());
}
return-1;
}
publicintdelete(String s) //数据库删除
{
try{
returnstmt.executeUpdate(s);
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"执行删除语句出错n"+e.toString());
}
return-1;
}
publicintupdate(String s) //数据库更新
{
try{
inttemp=stmt.executeUpdate(s);
returntemp;
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"执行修改语句出错n"+e.toString());
}
return-1;
}
publicResultSet getResultSet() //返回查询结果集
{
returnrs;
}
publicvoidcloseConnection() //关闭连接
{
try{
stmt.close();
con.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"关闭数据库连接时错误n"+e.toString());
}
}
}
使用实例:
查询:
String sql="";
Mysql mysql = newMysql();//建立连接数据库类的对象,Mysql就是上边类名
sql+="select * from login where stumber='"+jTextFieldUser.getText()+"' and spassword='"+jPasswordField.getText()+"'";//查询语句
mysql.search(sql);//调用函数
ResultSet rs = mysql.getResultSet();//返回数据集
try
{
while(rs.next())//遍历
{
stumber=rs.getString(1);//在一条记录集中去除某个位置的值
}
}catch(SQLException ex)//异常处理{}
剩下的增加,删除,修改,它们三格式一样:
修改: sql = "update login set spassword ='"+ jPasswordFieldnew.getText() +"' where stumber = stumber";
intflag = mysql.update(sql);
判断如果flag大于0,那么就执行成功!
Class.forName("com.mysql.jdbc.Driver");
2、创建连接
Connection conn=DriveManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "账号", "密码");
3、获取语句
Statement stmt=conn.createStatement();
4、操作数据
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
String user = "root" ,password = "mysql",url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
Connection connection = DriverManager.getConnection(url, user, password);
//增加
String sql = "insert into t1(id,name) values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, "123123123");
statement.setString(2, "jdbc");
System.out.println(statement.executeUpdate());
//查询
sql = "select * from t1";
Statement query = connection.createStatement();
ResultSet rs = query.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("id")+","+rs.getString("name"));
}
//修改
sql = "update t1 set name = ? where id = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "java");
statement.setString(2, "123123123");
System.out.println(statement.executeUpdate());
//删除
sql = "delete from t1 where id = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "123123123");
System.out.println(statement.executeUpdate());
connection.close();
}
注意,需要引入mysql-connector-java-x.x.x.jar