JDBC连接MySQL5.7的⽅法
2.从下载的⽂件中取出mysql-connector-java-5.1.31-bin.jar,放到⼯程中,并导⼊路径
⽅法:右击⼯程名->Build Path->Configure Build Path,选择Add 到mysql-connector-java-5.1.31-bin.jar所在的位置,然后将驱动包加载到项⽬中,
3.写个例⼦测试⼀下
package testmysql;
import java.sql.*;
public class Test {
public static void main(String[] args) {
String driver = "sql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3306/student";
Connection con = null;
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("Connect Successfull.");
System.out.println("Cant't load Driver");
}
try
{
Connection(URL,"root","root");
System.out.println("Connect Successfull.");
}
catch(Exception e)
{
System.out.println("Connect fail:" + e.getMessage());
}
}mysql下载jar包
}
连接上数据库之后,可以根据表中的内容进⾏数据库表的查询,⾸先表中要有内容,将⼀些信息输⼊到表中之后即可使⽤SQL 语⾔进⾏查询
import java.sql.*;
public class Main {
public static void main(String[] args) {
String driver = "sql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3306/xiaolu";
Connection con = null;
ResultSet rs = null;
Statement st = null;
String sql = "select * from student";
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e)
{
// System.out.println("Connect Successfull.");
System.out.println("Cant't load Driver");
}
try
{
Connection(URL,"root","root");
ateStatement();
uteQuery(sql);
if(rs!=null) {
ResultSetMetaData rsmd = rs.getMetaData();
int countcols = ColumnCount();
for(int i=1;i<=countcols;i++) {
if(i>1) System.out.print(";");
System.out.ColumnName(i)+" ");
}
System.out.println("");
()) {
System.out.String("sno")+" ");
System.out.String("sname")+" ");
System.out.String("ssex")+" ");
System.out.String("sage")+" ");
System.out.String("sdept")+" ");
}
}
//System.out.println("Connect Successfull.");
System.out.println("ok");
rs.close();
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("Connect fail:" + e.getMessage());
}
}
}
关于JDBC连接MySQL5.7的⽂章就介绍到这,其他的可以查下其它相关⽂章。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论