站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题java+eclipse 连mysql

java+eclipse 连mysql

摘要: java+eclipse 连mysql


java+eclipse 连mysql需要注意什么??????
我是个新手,最近做学生成绩管理系统,问问大虾,如何连mysql数据库,需要下哪些东西(已装了eclips,mysql,mysql_front),
高手指教跪谢 跪谢!


还需要个jar包,mysql-connector-java-3.1.8-bin.jar,把它放到你的classpath中

装好后可以测试一下能不能连上,命令行mysql –h localhost –u root –p

另外简单的连接语句写上就OK了,还是很轻松的 :)

String driverName="com.mysql.jdbc.Driver";
String user="XXX";
String userPasswd="XXXX";
String dbName="db";
String tableName="dbtest";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;

Class.forName("com.mysql.jdbc.Driver");

Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);

stament.close();
connection.close();



import java.sql.*;

public class UseDriver {
public static void main(String[] args)
{
String url="jdbc:mysql://localhost/database_name";

String userName="root";
String password="";
String sql=null;
Connection conn=null;
Statement stmt=null;
try
{
Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e)
{
System.err.print("ClassNotFoundException");
}

try
{
conn=DriverManager.getConnection(url,userName,password);
stmt=conn.createStatement();

sql="select * from student";

ResultSet rs=stmt.executeQuery(sql);

while(rs.next())
{
String id=rs.getString(1);
String name=rs.getString(2);
String address=rs.getString(3);
String birthdate=rs.getString(4);
System.out.println(id+" "+name+" "+address+" "+birthdate);
}

rs.close();
stmt.close();

}catch(SQLException e)
{
System.err.println("Close SQLException");
}
finally
{
try
{
if(conn!=null) conn.close();

}catch(SQLException e)
{
System.err.println("Close SQLException");
}
}

}

}



忘了说,得先在mysql下建个表student(id,name,address,birthdate)


↑返回目录
前一篇: 请问一个问题为什么在java中xpath没法解决啊
后一篇: 怎么样获取Eclipse的Hibernate、Struts的插件呀?