日期:2014-05-20 浏览次数:20815 次
package sample; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MysqlLianjie { public static void main(String[] args) throws ClassNotFoundException { String Driver = "com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/kb"; String username = "root"; String password = "long"; Connection con = null; Statement s = null; ResultSet rs = null; try { Class.forName(Driver); con = DriverManager.getConnection(url, username, password); System.out.println("数据库连接成功!"); s = con.createStatement(); // rs = s.executeQuery("select * from users"); String query = "create table consumer2(model INTEGER(4),speed INTEGER(4), ram INTEGER(4),hd INTEGER(4),price INTEGER(6))"; s.executeUpdate(query); System.out.println("创建表成功!"); } catch (SQLException e) { System.out.println("SQLException:" + e.getMessage()); } } }
------解决方案--------------------
在Eclipse中需要添加build path,然后add external jars,这样classpath就不用配置了。