如何在Java程序中asp访问mysql数据库库中的数据并进行简单的操作

java访问mysql数据库的方法
1、下载接口程序包-connector-java-5.0.8-bin.jar 下载地址
(1)加载驱动
(2)编程连接操作
(3)返回结果处理
import java.sql.*;
public class Access2Database{
public Connection getConn(){
Connection conn=
Class.forName(&com.mysql.jdbc.Driver&);
String url=&jdbc:mysql://localhost:3306/mytest&;
String user=&root&;
String password=&111&;
conn=DriverManager.getConnection(url, user, password);
if(conn!=null){
System.out.println(&The connection to database is successful!&);
}catch(Exception e){
e.printStackTrace();
public ResultSet getResultSet(Statement stam,String sql){
ResultSet res=
res=stam.executeQuery(sql);
} catch (SQLException e){
e.printStackTrace();
void showResultSet(ResultSet res){}
}import java.sql.*;
public class GetConnection{
public static void main(String[] args){
Access2Database adb=new Access2Database();
Connection conn=adb.getConn();
Statement stam=
stam = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
//show resultset
String sql=&select *&;
ResultSet res=adb.getResultSet(stam, sql);
System.out.println(&name\tmajor\tscore&);
while(res.next()){
String name,
name=res.getString(1);
major=res.getString(2);
score=res.getInt(3);
System.out.println(name+&\t&+major+&\t&+score);
} catch (SQLException e) {
e.printStackTrace();
res.close();
}catch(SQLException e){
e.printStackTrace();
//insert something into table
sql=&insert into student(name,major,score) values('f','Chinese','70');&;
stam.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
//delete something from the table
sql=&delete from student where name='f';&;
stam.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
//change the data int the table
sql=&update student set score=100 where name='a' and major='Chinese'&;
stam.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
//prepared statement
sql=&select * from student where name=?&;
PreparedStatement pstam=
pstam=conn.prepareStatement(sql);
pstam.setString(1, &a&);
res=pstam.executeQuery();
System.out.println(&**********************&);
while(res.next()){
String name,
name=res.getString(1);
major=res.getString(2);
score=res.getInt(3);
System.out.println(name+&\t&+major+&\t&+score);
} catch (SQLException e) {
e.printStackTrace();
//release the resource of the program
res.close();
pstam.close();
stam.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
按需调整代码即可本帖子已过去太久远了,不再提供回复功能。Java对MySQL数据库进行连接、查询和修改
1. 连接数据库
  (1) 下载Mysql连接驱动
网址:&&,下载后放在F:\博士科研资料\数据库学习\mysql相关程序文件中,解压。
  (2) 加载JDBC驱动
操作方法:在Eclipse中,选中相应的工程,点击Project-Properties中的Java Build
Path,在Libraries中增加mysql-connector-java-5.1.21-bin.jar,点OK
2. search table
import java.sql.*;
class SelectTable {
& public static void main(String[]
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
& & String
url="jdbc:mysql://localhost:3306/aniu"; &
&//JDBC的URL &
& & & Connection
conn = DriverManager.getConnection(url, &
&"root","");
& & & Statement
stmt = conn.createStatement(); //创建Statement对象
& & & String sql
= "select * from stu"; &
&//要执行的SQL
& & & ResultSet
rs = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
& & while (rs.next()){
& System.out.print(rs.getInt(1) + "\t");
& System.out.print(rs.getString(2) + "\t");
& System.out.print(rs.getInt(3) + "\t");
& System.out.println();
& & rs.close();
& & stmt.close();
& & conn.close();
}catch(Exception e)
& & e.printStackTrace();
修改删除数据
import java.sql.*;
public class UpdateDeleteDemo {
& & public static void
main(String[] args)throws Exception{
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
url="jdbc:mysql://localhost:3306/aniu"; &
&//JDBC的URL &
& & & Connection
& & conn =
DriverManager.getConnection(url, &
&"root","");
& & & Statement
stmt = conn.createStatement(); //创建Statement对象
//查询数据的代码
& & & String sql
= "select * from stu"; &
&//要执行的SQL
& & & ResultSet
rs = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
& & while (rs.next()){
& System.out.print(rs.getInt(1) + "\t");
& System.out.print(rs.getString(2) + "\t");
& System.out.print(rs.getInt(3) + "\t");
& System.out.println();
//修改数据的代码
& & & String
sql2 = "update stu set name=? where number=?";
PreparedStatement pst = conn.prepareStatement(sql2);
pst.setString(1,"8888");
pst.setInt(2,198);
pst.executeUpdate();
//删除数据的代码
& & & String
sql3 = "delete from stu where number=?";
& & & pst =
conn.prepareStatement(sql3);
pst.setInt(1,701);
pst.executeUpdate();
& & & ResultSet
rs2 = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
& & & while
(rs.next()){
System.out.print(rs2.getInt(1) + "\t");
System.out.print(rs2.getString(2) + "\t");
System.out.print(rs2.getInt(3) + "\t");
& & System.out.println();
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
& & e.printStackTrace();
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Java中使用JDBC操作数据库简单实例
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Java中使用JDBC操作数据库简单实例,本文以Mysql为例介绍使用Java JDBC操作数据库的6个步骤,需要的朋友可以参考下
好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC。
1.使用Java JDBC操作数据库一般需要6步:
(1)建立JDBC桥接器,加载数据库驱动;
(2)连接数据库,获得Connection对象(使用数据库连接地址,用户名,密码);
(3)获得数据库Statement对象;
(4)执行数据库操作;
(5)读取结果;
(6)关闭数据库连接;
2.使用Java JDBC操作数据库(mysql)代码:
连接mysql数据库,需要导入mysql数据库jar包,本代码使用mysql-connector-java-5.1.18-bin.jar。
import java.sql.C
import java.sql.DriverM
import java.sql.S
import java.sql.ResultS
public class MyTest {
public static void main(String args[]) {
Connection con =
Statement st =
ResultSet rs =
// 获得MySQL驱动的实例
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 获得连接对象(提供:地址,用户名,密码)
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/Weather","root", "root");
if (!con.isClosed())
System.out.println("Successfully connected ");
System.out.println("failed connected");
//建立一个Statement,数据库对象
st = con.createStatement();
// 运行SQL查询语句
rs = st.executeQuery("select * from Weather.question_type_1;");
// 读取结果集
while (rs.next()) {
System.out.println("column1:"+rs.getInt(1));
System.out.println("column2:"+rs.getString(2));
System.out.println("column3:"+rs.getString(3));
System.out.println("column4:"+rs.getString(4));
// 关闭链接
con.close();
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具}

我要回帖

更多关于 远程访问mysql数据库 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信