文件名称:java连接Mysql
文件大小:1015B
文件格式:JAVA
更新时间:2014-01-31 13:48:37
Mysql
使用java连接mysql数据库代码 package com.tsinghua; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class conn { private static Connection conn=null; private static String url="jdbc:mysql://localhost:3306/root"; private static String driver="com.mysql.jdbc.Driver"; public static void main(String[] args) { if(conn==null) { try { Class.forName(driver); conn=DriverManager.getConnection(url,"root","root"); if(conn!=null) { Statement s=conn.createStatement(); ResultSet rs=s.executeQuery("select * from stu"); while(rs!=null&&rs.next()) { System.out.println(rs.getInt("sid")+" "+rs.getString(2)+" "+rs.getString(3)); } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }