package dbtest;
import java.sql.*;
public class DataConnection {
public static void main(){
String driver="com.MySQL.jdbc.Driver";
String url="jdbc:MySQL://localhost/mydatabase";
String user="root";
String pwd="123";
try{
Class.forName(driver);
Connection con=DriverManager.getConnection(url,user,pwd);
String insert="insert friends(id,name) values(?,?)";
PreparedStatement prestatement=con.prepareStatement(insert);
String []names={"Rachel","Rose","Phoebe","Monica","Chandler","Joey"};
for(int i=0;i<6;i++)
{
prestatement.setInt(1, i);
prestatement.setString(2,names[i]);
prestatement.execute();
}
String query="Select *from friends";
ResultSet result=prestatement.executeQuery(query);
while(result.next())
{
System.out.println(result.getString("name"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
运行这段程序后,出现在console的如下:
<ConnectionProperties>
<PropertyCategory name="Connection/Authentication">
<Property name="user" required="No" default="" sortOrder="-2147483647" since="all versions">
The user to connect as
</Property>
<Property name="password" required="No" default="" sortOrder="-2147483646" since="all versions">
The password to use when connecting
</Property>
<Property name="socketFactory" required="No" default="com.mysql.jdbc.StandardSocketFactory" sortOrder="4" since="3.0.3">
The name of the class that the driver should use for creating socket connections to the server. This class must implement the interface 'com.mysql.jdbc.SocketFactory' and have public no-args constructor.
</Property>
<Property name="connectTimeout" required="No" default="0" sortOrder="9" since="3.0.1">
Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to '0'.
为什么没有输出:
The result is:
Rose
Phoebe
Monica
Chandler
Joey
Rachel
import java.sql.*;
public class DataConnection {
public static void main(){
String driver="com.MySQL.jdbc.Driver";
String url="jdbc:MySQL://localhost/mydatabase";
String user="root";
String pwd="123";
try{
Class.forName(driver);
Connection con=DriverManager.getConnection(url,user,pwd);
String insert="insert friends(id,name) values(?,?)";
PreparedStatement prestatement=con.prepareStatement(insert);
String []names={"Rachel","Rose","Phoebe","Monica","Chandler","Joey"};
for(int i=0;i<6;i++)
{
prestatement.setInt(1, i);
prestatement.setString(2,names[i]);
prestatement.execute();
}
String query="Select *from friends";
ResultSet result=prestatement.executeQuery(query);
while(result.next())
{
System.out.println(result.getString("name"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
运行这段程序后,出现在console的如下:
<ConnectionProperties>
<PropertyCategory name="Connection/Authentication">
<Property name="user" required="No" default="" sortOrder="-2147483647" since="all versions">
The user to connect as
</Property>
<Property name="password" required="No" default="" sortOrder="-2147483646" since="all versions">
The password to use when connecting
</Property>
<Property name="socketFactory" required="No" default="com.mysql.jdbc.StandardSocketFactory" sortOrder="4" since="3.0.3">
The name of the class that the driver should use for creating socket connections to the server. This class must implement the interface 'com.mysql.jdbc.SocketFactory' and have public no-args constructor.
</Property>
<Property name="connectTimeout" required="No" default="0" sortOrder="9" since="3.0.1">
Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to '0'.
为什么没有输出:
The result is:
Rose
Phoebe
Monica
Chandler
Joey
Rachel
是不是没有导入相应的包,或者有ClassPath有重名的包和类
好像这里错了String url="jdbc:MySQL://localhost:3306/mydatabase";