设为首页 加入收藏

TOP

jdbc连接mysql
2015-11-21 01:26:12 来源: 作者: 【 】 浏览:0
Tags:jdbc 连接 mysql

?

Test.java

?

 
 
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();
		}

	}

}
Main.java

public class Main {

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

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

	}

}

?

输出 : finish


\

记得要加载jar别忘了

\

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇09-SQLite之join 下一篇关于VBS连接MySQL与连接Excel

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: