我先在mysql创建了了 create database jdbc; 建好了表。mysql 没有账号 密码是1
然后Eclipse里代码如下
package jdbc;
import java.sql.*;
public class Labl {
public static void main(String[] args) {
String url="jdbc:mysql://localhost:3306/jdbc";
String username=" ";
String password="1";
String driver="com.mysql.jdbc.Driver";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
String sqli="insert into member values(1001,'zhangxing','1987',23,'12240532')";
int count=stmt.executeUpdate(sqli);
}catch (Exception e) {
}
finally{
try {
rs.close();
stmt.close();
con.close();
} catch (Exception e1) {
}
}
}}但是在con=DriverManager.getConnection(url,username,password);
进行不下去了,是不是我url写错了,求高手指教!!
然后Eclipse里代码如下
package jdbc;
import java.sql.*;
public class Labl {
public static void main(String[] args) {
String url="jdbc:mysql://localhost:3306/jdbc";
String username=" ";
String password="1";
String driver="com.mysql.jdbc.Driver";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
String sqli="insert into member values(1001,'zhangxing','1987',23,'12240532')";
int count=stmt.executeUpdate(sqli);
}catch (Exception e) {
}
finally{
try {
rs.close();
stmt.close();
con.close();
} catch (Exception e1) {
}
}
}}但是在con=DriverManager.getConnection(url,username,password);
进行不下去了,是不是我url写错了,求高手指教!!
请查看回复信息后,再回帖,楼主都在后面说明系统默认的还是有一个username=“root”,所以username不是null。
既然知道username="root",为什么在程序里写成username=" "呢?
import java.sql.Connection;
import java.sql.DriverManager;import com.microsoft.sqlserver.jdbc.SQLServerDriver;public class Sqlcon {
public static void main(String[] args) throws Exception{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=studentinfo","sa", "123");
}}或者这样sqlserver2005
//将不能改变的字段设置为常量
private static final String CLASSNAME="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;databasename=数据库名";
private static final String USERNAME="数据库用户名";
private static final String PASSWORD="登录密码";
//连接方法
public Connection getCon(){
try {
Class.forName(CLASSNAME);
return DriverManager.getConnection(URL, USERNAME,PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
希望能帮到你mysql我们没有用,