Home > Database > Mysql Tutorial > jdbc连接mysql_MySQL

jdbc连接mysql_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-30 17:11:18
Original
1713 people have browsed it

 

Test.java
Copy after login

Copy after login
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;

public class Test {
	static Connection con; // 声明Connection对象
	static Statement sql; // 声明Statement对象
	static ResultSet res;

	public Connection getConnection() { // 连接数据库方法
		try {
			  Class.forName( "org.gjt.mm.mysql.Driver" );
			con = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/test", "root",
					"0000");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return con; // 返回Connection对象
	}

	// ///////////////

	public static void insertdata() {
		Test c = new Test();
		con = c.getConnection();

		try {
			sql = con.createStatement();

			sql.executeUpdate("  insert  into  stu  values(12,'Shara' ) ;");
			

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
Copy after login
Main.java
Copy after login

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Test.insertdata();
		System.out.println("finish");

	}

}
Copy after login

 

输出 : finish


\

记得要加载jar别忘了

\

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template