最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

java访问mysql数据库的方法_MySQL

来源:动视网 责编:小采 时间:2020-11-09 19:40:54
文档

java访问mysql数据库的方法_MySQL

java访问mysql数据库的方法_MySQL:1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 import java.sql.*; public class Access2Database{ public Connection getC
推荐度:
导读java访问mysql数据库的方法_MySQL:1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 import java.sql.*; public class Access2Database{ public Connection getC


1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址

2、编程

(1)加载驱动

(2)编程连接操作

(3)返回结果处理

编程示例

import java.sql.*;

public class Access2Database{
	public Connection getConn(){
	Connection conn=null;
	try{
	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();
	}
	return conn;
	}
	
	public ResultSet getResultSet(Statement stam,String sql){
	ResultSet res=null;
	try {
	res=stam.executeQuery(sql);
	} catch (SQLException e){
	e.printStackTrace();
	}
	return res;
	}
	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=null;
	try {
	stam = conn.createStatement();
	} catch (SQLException e1) {
	e1.printStackTrace();
	}
	
	//show resultset
	String sql="select * from student;";
	ResultSet res=adb.getResultSet(stam, sql);
	try {
	System.out.println("name\tmajor\tscore");
	while(res.next()){
	String name,major;
	int score;
	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();
	}
	try{
	res.close();
	}catch(SQLException e){
	e.printStackTrace();
	}
	
	//insert something into table
	sql="insert into student(name,major,score) values('f','Chinese','70');";
	try {
	stam.execute(sql);
	} catch (SQLException e) {
	e.printStackTrace();
	}
	
	//delete something from the table
	sql="delete from student where name='f';";
	try{
	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'";
	try{
	stam.executeUpdate(sql);
	}catch(SQLException e){
	e.printStackTrace();
	}
	
	//prepared statement
	sql="select * from student where name=?";
	PreparedStatement pstam=null;
	try {
	pstam=conn.prepareStatement(sql);
	pstam.setString(1, "a");
	res=pstam.executeQuery();
	System.out.println("**********************");
	while(res.next()){
	String name,major;
	int score;
	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
	try{
	res.close();
	pstam.close();
	stam.close();
	conn.close();
	}catch(SQLException e){
	e.printStackTrace();
	}
	}
}
按需调整代码即可

文档

java访问mysql数据库的方法_MySQL

java访问mysql数据库的方法_MySQL:1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址 2、编程 (1)加载驱动 (2)编程连接操作 (3)返回结果处理 编程示例 import java.sql.*; public class Access2Database{ public Connection getC
推荐度:
标签: 连接 方法 的方法
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top